我的应用中有两个 ViewControllerViewController1.m
和ViewController2.m
.
在AppDelegate
我有这个代码。
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[ViewController1 alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
}
else
{
self.viewController = [[ViewController1 alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
在 ViewController1.m 中,我添加了一个按钮,在单击按钮时,我正在显示另一个视图控制器ViewController2.m
,如下所示:
ViewController2 * obj = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:obj.view];
在loadView
ViewController2.m 中,我添加了另一个像这样的按钮
NSLog(@"\n Load view called");
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(onButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Back to previous view" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
当我运行我的应用程序时,单击 ViewController1.m 中的按钮时,应用程序挂起,并且loadView
ViewController2.m 开始被无限调用。
我不知道这个问题背后的原因,我只是想在单击按钮时加载另一个 ViewController 而我没有使用任何导航控制器。
有人可以指出这个问题背后的原因吗?