0

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

我在应用程序中添加视图: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(code=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;
}

这解决了错误。

4

1 回答 1

0

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

也许您可以让 IntroView 视图控制器成为应用程序委托类的属性,因此 ARC 不会释放它。

就我个人而言,我不会在应用程序代理创建视图和控制器时胡思乱想,而是让我的目标设置和我的 NIB 决定这一点。我想你这样做是因为你想要一些介绍屏幕。如果我想要一个屏幕,我会让我的主视图控制器继续并呈现我想要的任何介绍。这样,当介绍被关闭(或弹出,取决于您是推送还是呈现模式)时,我的主视图控制器仍然位于顶部(并且像 popToRootViewController 这样的有用方法可以完美运行)。

于 2012-05-29T17:10:45.970 回答