开发者

How to show UIViewController properly?

开发者 https://www.devze.com 2023-04-04 13:28 出处:网络
I need to show a view controller and have this current code: SavedFiles *savedFilesVC = [[[SavedFiles alloc] init] autorelease];

I need to show a view controller and have this current code:

    SavedFiles *savedFilesVC = [[[SavedFiles alloc] init] autorelease];
    [self presentModalViewController:savedFilesVC animated:YES];   

Although it gets this error:

2011-09-12 18:38:45.808 iDHSB[248:707] -[iDHSB_MobileAppDelegate presentModalViewController:animated:]: unrecognized selector     
sent to instance 0x1bb880
2011-09-12 18:38:45.825 iDHSB[248:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-   
[iDHSB_MobileAppDelegate presentModalViewController:animated:]: unrecognized selector sent to instance 0x1bb880'
*** Call stack at first throw:
(
0   CoreFoundation                      0x3583064f __exceptionPreprocess + 114
1   libobjc.A.dylib                     0x36583c5d objc_exception_throw + 24
2   CoreFoundation                      0x358341bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
3   CoreFoundation                      0x35833649 ___forwarding___ + 508
4   CoreFoundation                      0x357aa180 _CF_forwarding_prep_0 + 48
5   iDHSB                               0x00007671 -[iDHSB_MobileAppDelegate actionSheet:clickedButtonAtIndex:] + 140
6   UIKit                               0x3203c03d -[UIActionSheet(Private) _buttonClicked:] + 192
7   CoreFoundation                      0x357a0571 -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
8   UIKit                               0x31f39ec9 -[UIApplication sendAction:to:from:forEvent:] + 84
9   UIKit                               0x31f39e69 -[UIApplication sendAction:toTarget:fromSender:forEvent:] 开发者_开发问答+ 32
10  UIKit                               0x31f39e3b -[UIControl sendAction:to:forEvent:] + 38
11  UIKit                               0x31f39b8d -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
12  UIKit                               0x31f3a423 -[UIControl touchesEnded:withEvent:] + 342
13  UIKit                               0x31f38bf5 -[UIWindow _sendTouchesForEvent:] + 368
14  UIKit                               0x31f3856f -[UIWindow sendEvent:] + 262
15  UIKit                               0x31f21313 -[UIApplication sendEvent:] + 298
16  UIKit                               0x31f20c53 _UIApplicationHandleEvent + 5090
17  GraphicsServices                    0x31df7e77 PurpleEventCallback + 666
18  CoreFoundation                      0x35807a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
19  CoreFoundation                      0x3580983f __CFRunLoopDoSource1 + 166
20  CoreFoundation                      0x3580a60d __CFRunLoopRun + 520
21  CoreFoundation                      0x3579aec3 CFRunLoopRunSpecific + 230
22  CoreFoundation                      0x3579adcb CFRunLoopRunInMode + 58
23  GraphicsServices                    0x31df741f GSEventRunModal + 114
24  GraphicsServices                    0x31df74cb GSEventRun + 62
25  UIKit                               0x31f4bd69 -[UIApplication _run] + 404
26  UIKit                               0x31f49807 UIApplicationMain + 670
27  iDHSB                               0x0000232d main + 48
28  iDHSB                               0x000022f8 start + 40
)
terminate called after throwing an instance of 'NSException'
[Switching to process 11779 thread 0x0]

Why is this happening?

What do I need to do?

Thanks,

James


Try this

[self.navigationController pushViewController:viewController animated:YES];

or

[self.view addsubview.viewcontroller.view];


You need something like this in your AppDelegate to load the first ViewController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Add the navigation controller's view to the window and display

    self.window.rootViewController = myController;
    [self.window makeKeyAndVisible];
    return YES;
}

Loading and initializing the view controller happens (typically) in the MainWindow.xib file. You cannot call presentModalViewController from an AppDelegate and you cannot just init your controller, you need initFromNib in most cases..


You cannot use presentModalViewController:animated: directly from an AppDelegate. It must be presented from an instance of UIViewController. This is the cause of the error.


[[iDHSB_MobileAppDelegate mainViewController] presentModalViewController:savedFilesVC  animated:YES]

you need to create a UIViewController say for ex mainViewController and connect it into xib.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号