我創建了一個全新的項目,並在視圖中創建了一個帶有按鈕的新視圖控制器。單擊一個按鈕結果exe_bad_action
我加入在應用程序中的視圖中: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。
感謝
UPDATE
創建名爲 「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;
}
這解決了錯誤。
感謝您的回答。閱讀您的回覆後,我更新了代碼並解決了錯誤。謝謝。我在簡介和ARC的範圍內迷路了。 – iJK
是的,當應用程序加載時(僅限第一次),我需要一個介紹屏幕。從介紹屏幕中,用戶可以點擊按鈕登錄或註冊,註冊/登錄後,我想加載一個帶有表格視圖的標籤欄控制器。你在這種情況下推薦什麼?感謝您進入細節 – iJK
我想我的主視圖控制器的viewDidLoad確定是否需要此介紹/登錄屏幕,然後呈現該視圖控制器(通過模態或如果您使用導航控制器推送它)。 – Rob