单击按钮会导致exe_bad_action

时间:2012-05-29 17:01:24

标签: ios ios5 automatic-ref-counting

我创建了一个全新的项目,并在视图中创建了一个带有按钮的新视图控制器。

我在应用程序中添加了视图:didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];


    BOOL introDisplayed = [[NSUserDefaults standardUserDefaults] boolForKey:kIntroScreenSeenByUser];

    if(introDisplayed)
    {

    }
    else 
    {
        IntroView *introView = [[IntroView alloc] initWithNibName:@"IntroView" bundle:nil];
        [self.window addSubview:introView.view];
    }

    [self.window makeKeyAndVisible];

    return YES;
}

.h文件

@interface IntroView : UIViewController 


@property (weak, nonatomic) IBOutlet UIButton *clickMe;
- (IBAction)clicked:(id)sender;

@end

.m文件

#import "IntroView.h"

@interface IntroView ()
@end

@implementation IntroView
@synthesize clickMe;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setClickMe:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)clicked:(id)sender {
    NSLog(@"clicked");
}
@end

单击该按钮会导致EXC_BAD_ACCESS(代码= 2错误。有任何想法吗?我正在使用ARC。

由于

更新

在名为“introViewController”的应用程序委托上创建了一个公共属性,并更改了应用程序:didFinishLaunchingWithOptions

@synthesize introViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];


    BOOL introDisplayed = [[NSUserDefaults standardUserDefaults] boolForKey:kIntroScreenSeenByUser];

    introViewController = [[IntroView alloc] initWithNibName:@"IntroView" bundle:nil];
    if(introDisplayed)
    {

    }
    else 
    {
        [self.window addSubview:introViewController.view];
    }

    [self.window makeKeyAndVisible];

    return YES;
}

这解决了错误。

1 个答案:

答案 0 :(得分:0)

您正在创建IntroView控制器,并将其视图添加为子视图,但控制器本身已发布。我不认为添加视图控制器的视图(然后让ARC丢弃控制器本身)是创建视图的可接受方式。

也许你可以让IntroView视图控制器成为app delegate类的属性,因此它不会被ARC发布。

就我个人而言,我并没有围绕app委托创建视图和控制器,而是让我的目标设置和我的NIB决定这一点。我认为你这样做是因为你想要一些介绍屏幕。如果我想要一个屏幕,我会让我的主视图控制器继续前进并呈现我想要的任何介绍。这样当介绍被解散(或弹出时,取决于你是否按模式推送),我的主视图控制器仍然位于顶部(并且popToRootViewController等有用的方法工作正常)。