Switching view controllers crashes with an uncaught exception
-
05-10-2019 - |
문제
I'm trying to switch views between view controllers in my iPhone app, but it crashed. Basically I'm switching from mainScreen to test.
I get an error in the debugger:
0x01d6a000 <+0000> push %ebp
0x01d6a001 <+0001> mov %esp,%ebp
0x01d6a003 <+0003> int3
0x01d6a004 <+0004> leave (HIGHLIGHTED)
0x01d6a005 <+0005> ret
0x01d6a006 <+0006> nopw %cs:0x0(%eax,%eax,1)
mainscreen.h
#import <UIKit/UIKit.h>
@interface MainScreen : UIViewController {
}
-(IBAction)btnFirstPage:(id)sender;
@end
mainscreen.m
#import "MainScreen.h"
#import "test.h"
@implementation MainScreen
-(IBAction)btnFirstPage:(id)sender{
test1 = [[test1 alloc]
initWithNibName:@"test" (test may not respond to -alloc)
bundle:nil];
[self.view addSubview:test1.view];
/* etc. */
test.h
#import <UIKit/UIKit.h>
@interface test : UIViewController {
}
@end
해결책
This looks odd: test1 = [[test1 alloc] ...]
. You are sending the alloc
message to a variable, which I assume is initially the null pointer, and is thus being quietly ignored. You should invoke alloc
on test1
's class type, not test1
itself.
다른 팁
test1 = [[test alloc] initWithNibName:@"test" undle:nil]; //here it must be test. not test1
When you create objects from classes you have to use alloc and allocWithZone static methods use in the NSObject class. So you have to use Class name. Not the variable name.(test1)
제휴하지 않습니다 StackOverflow