【问题标题】:SIGABRT When going back to MainViewControllerSIGABRT 回到 MainViewController 时
【发布时间】:2013-03-31 11:07:11
【问题描述】:

在我的应用程序中,当用户按下按钮时,我想在不同的视图中生成有关该主题的信息。例如,当用户按下 cat 按钮时,我想要一个新视图来显示标题和描述。但是,当他们按 dog 时,它会转到相同的视图,但会更改信息。

firstViewController.h

#import "secondview.h"

@interface ViewController :UIViewController 
{
    secondview *secondviewData;
    IBOutlet UITextField *textfield;
}

@property (nonatomic, retain)secondview*secondviewData;

-(IBAction)passdata:(id)sender; 

@end

firstViewController.m

#import "ViewController.h"
#import "secondview.h"

@implementation ViewController

@synthesize secondviewData;

-(IBAction)passdata:(id)sender 
{
    secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];  
    self.secondviewData = second; 
    secondviewData.passedValue = @"dog Info";
    [self presentModalViewController:second animated:YES];
}

@end

secondViewController.h

@interface secondview :UIViewController 
{
    IBOutlet UILabel *label;  
    NSString *passedValue;
}

@property (nonatomic, retain)NSString *passedValue;

-(IBAction)back:(id)sender;

@end

SecondViewController.m

#import "ViewController.h"

@implementation secondview

@synthesize passedValue;

-(IBAction)back:(id)sender 
{
    ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:second animated:YES];
} 

- (void)viewDidLoad
{
    label.text = passedValue;
    [super viewDidLoad];
} 

@end

当我执行这段代码时,它会出现SIGABRT

【问题讨论】:

  • 好吧,看看你的控制台(Cmd-Shift-R)。

标签: objective-c cocoa-touch sigabrt


【解决方案1】:

如果您的下一个视图与当前视图分离,您可以使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"nextViewSegue"]){

        nextViewController *nextViewObj  = segue.destinationViewController;
        nextViewObj.someNSString = @"the string you wanna to pass";
    }
    else if ([segue.identifier isEqualToString:@"nextViewSegue2"]){
        otherNextViewController *otherNextViewObj  = segue.destinationViewController;
        otherNextViewObj.someNSString = @"the string you wanna to pass";
    }
}

---------------还有另一种方式:-------- -------------------

您可以使用全局变量来传递参数。

创建一个类

.h

   @interface iXxxxxxGlobal : NSObject
    {
        NSString *globalString;

    }
    @property (nonatomic,retain)NSString *globalString;
    +(iXxxxxxGlobal *)getInstance;
    -(void) updateSetting;
    @end

.m

#import "iXxxxxxGlobal.h"

@implementation iXxxxxxGlobal

@synthesize globalString;

static iXxxxxxGlobal *instance = nil;


+(iXxxxxxGlobal *)getInstance
{
    if(instance ==nil)
    {
        instance = [iXxxxxxGlobal new];
        // Get initial value from Preferences and Settings.
        // Use <<Preferences and Settings>> 

    }
    return instance;
}

-(void)updateSetting
{
    //Do something to update <<Preferences and Settings>> 
}

@end

每次使用全局变量时,只需这样做:

iXxxxxxGlobal *iXxxxxxGlobalObj=[iXxxxxxGlobal getInstance];
iXxxxxxGlobalObj.globalString = @"your string";
NSString *localString = iXxxxxxGlobalObj.globalString;

【讨论】:

  • 这很棒,但我不使用情节提要。那我该怎么做呢?
  • 那么,你可以使用第二种方式:全局变量。
猜你喜欢
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多