将self.window.rootViewController设置为SIGABRT的原因

7

我正在尝试创建一个应用程序,您按下按钮后会出现一个UIAlertView,其中包含一个UIView,其中有3个自定义按钮。到目前为止,一切都可以工作。当我单击其中的3个按钮之一时,我想要更改UIImageView的图像,这也可以正常工作。问题是,每次尝试启动我的应用程序时,都会突然出现SIGABRT

SIGABRT发生在我的AppDelegate.m文件中的此行:

self.window.rootViewController = self.viewController;

如果有人能帮我,那就太好了。顺便说一下,我不太熟悉Xcode和Objective-C,所以我不知道为什么会出现这种情况。
以下是我的viewController.h:
#import UIKit/UIKit.h (i removed < > cause they were hiding everything inbetween in the post.)

@interface savingstatusViewController : UIViewController {

    UIView *loginView;
    UIImageView *statusImage;
    UIAlertView * MyTicketAlert;

}

- (IBAction)status:(id)sender;

@property (nonatomic, retain) IBOutlet UIView *loginView;    
@property (nonatomic, retain) IBOutlet UIImageView *statusImage;
@property (nonatomic, retain) IBOutlet UIAlertView * MyTicketAlert;

- (IBAction)green:(id)sender;
- (IBAction)yellow:(id)sender;
- (IBAction)red:(id)sender;

@end

这是我的viewController.m文件

#import "savingstatusViewController.h"

@implementation savingstatusViewController

@synthesize loginView;
@synthesize statusImage,MyTicketAlert;

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

 #pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setLoginView:nil];
    [self setStatusImage:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (IBAction)status:(id)sender
{
    MyTicketAlert = [[UIAlertView alloc] initWithTitle:nil message:nil  delegate:self cancelButtonTitle:nil
                                     otherButtonTitles: nil];
    [MyTicketAlert addSubview:loginView];
    [MyTicketAlert show];
    MyTicketAlert.frame = CGRectMake(0, 1000, 440, 110);
}

- (IBAction)green:(id)sender 
{
    statusImage.image = [UIImage imageNamed:@"etat_controle.png"];

     [MyTicketAlert dismissWithClickedButtonIndex:0 animated:YES];
}

- (IBAction)yellow:(id)sender
 {
    statusImage.image = [UIImage imageNamed:@"etat_attention.png"];

     [MyTicketAlert dismissWithClickedButtonIndex:0 animated:YES];
}

- (IBAction)red:(id)sender
 {
    statusImage.image = [UIImage imageNamed:@"etat_danger.png"];

     [MyTicketAlert dismissWithClickedButtonIndex:0 animated:YES];
}

@end

窗口和视图控制器初始化的代码在哪里?你能发一下吗? - fbrereto
我遇到了相同的问题 :( - iOS Monster
3个回答

5

iOS4之前的UIWindow没有rootViewController属性。如果您尝试在iOS 3或更早版本的设备上运行此代码,它将崩溃。

在AppDelegate中,您可以使用addSubview代替。

//self.window.rootViewController = self.viewController; // Only iOS >= 4
[self.window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
return YES;

或者,使用预处理器检查:"#if __IPHONE_OS_MIN_VERSION_REQUIRED>=__IPHONE_4_0" - aksommerville
在互联网上找了大约两个小时才找到这个。那里有很多垃圾,令人惊讶的答案第一次就起作用了,谢谢TumbleCow。当在不同版本的XCode中工作时,这是一个真正的助力。还要注意,如果你在使用旧版IOS,需要在xcode 4.?中更改armv7为armv6。 - user1149610

4
如果文件的拥有者视图未设置或设置不正确,也会发生这种情况。请确保您的ViewController.xib文件的File's Owner视图属性已设置为您的视图。我花了一段时间才弄清楚这个问题!

2

在MainWindow.xib中转到"TheNameOfYourProjetdelegate"。

在"TheNameOfYourProjetdelegate"的"outlets"中,您必须有viewController,然后进行"控件拖动"到窗口并运行应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接