我有兩個ViewControllers在我的應用程序ViewController1.m
和ViewController2.m
。loadview被稱爲無限次
在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];
在ViewController2.m的loadView
我加入另一個按鈕等這
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
Ø f ViewController2.m開始無限調用。
我不知道這個問題的背後,我只是想加載另一個ViewController的按鈕單擊,我沒有使用任何導航控制器。
有人能指出這個問題背後的原因嗎?
只看到方法堆棧跟蹤,你可以很容易地找到哪個方法或語句一次又一次地調用loadview – Iducool