我试图让我的应用程序从一个视图切换到另一个视图然后有一个后退按钮。第一步我没有遇到任何麻烦,但现在我想回到我的主视图控制器。
我试过这段代码:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
ViewController *main = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[main setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:main animated:YES];
还有:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[mainView setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainView animated:YES completion:NULL];
但两种方法都无法正常工作。使用方法一,我在最后一行'presentModalViewController:animated:' is deprecated: first deprecated in iOS6.0
收到错误
至于第二种方法,它只是简单的不起作用。我可以加载应用程序但是只要我按下后退按钮它就会崩溃:
*** First throw call stack:
(
0 CoreFoundation 0x0000000101947495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001016a699e objc_exception_throw + 43
2 UIKit 0x000000010070d2b7 +[UIStoryboard storyboardWithName:bundle:] + 542
3 Swindler 0x0000000100001827 -[SecondViewController backButton:] + 103
4 UIKit 0x0000000100254f06 -[UIApplication sendAction:to:from:forEvent:] + 80
5 UIKit 0x0000000100254eb4 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
6 UIKit 0x0000000100331880 -[UIControl _sendActionsForEvents:withEvent:] + 203
7 UIKit 0x0000000100330dc0 -[UIControl touchesEnded:withEvent:] + 530
8 UIKit 0x000000010028bd05 -[UIWindow _sendTouchesForEvent:] + 701
9 UIKit 0x000000010028c6e4 -[UIWindow sendEvent:] + 925
10 UIKit 0x000000010026429a -[UIApplication sendEvent:] + 211
11 UIKit 0x0000000100251aed _UIApplicationHandleEventQueue + 9579
12 CoreFoundation 0x00000001018d6d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
13 CoreFoundation 0x00000001018d65f2 __CFRunLoopDoSources0 + 242
14 CoreFoundation 0x00000001018f246f __CFRunLoopRun + 767
15 CoreFoundation 0x00000001018f1d83 CFRunLoopRunSpecific + 467
16 GraphicsServices 0x0000000103abef04 GSEventRunModal + 161
17 UIKit 0x0000000100253e33 UIApplicationMain + 1010
18 Swindler 0x0000000100001e43 main + 115
19 libdyld.dylib 0x0000000101fdf5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
非常感谢任何帮助!
答案 0 :(得分:0)
如果你想要后退按钮,而不是使用推送方法呈现视图控制器,这样它就会自动为你提供后退导航按钮
[self.navigationController pushViewController:mainView animated:YES];
否则手动在故事板中的第二个视图控制器中添加UIBarbuttonItem并在该按钮目标方法调用中
[self dismissViewControllerAnimated:YES completion:nil];