简体   繁体   中英

Dismissing a modal view controller

I have added code to my didFinishLaunchingWithOptions method to open a specific view when opening the app from a notification. I've added a "dismiss" button to the navigation bar for that view. I'm trying to get that button to take the user to the home screen after dismissing the modal view. Right now when the user taps the button, the app crashes. Does anyone here have any ideas?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // This prevents the UA Library from registering with UIApplcation by default when
    // registerForRemoteNotifications is called. This will allow you to prompt your
    // users at a later time. This gives your app the opportunity to explain the benefits
    // of push or allows users to turn it on explicitly in a settings screen.
    // If you just want everyone to immediately be prompted for push, you can
    // leave this line out.
    // [UAPush setDefaultPushEnabledValue:NO];

    //Create Airship options dictionary and add the required UIApplication launchOptions
    NSMutableDictionary *takeOffOptions = [NSMutableDictionary dictionary];
    [takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];

    // Call takeOff (which creates the UAirship singleton), passing in the launch options so the
    // library can properly record when the app is launched from a push notification. This call is
    // required.
    //
    // Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
    [UAirship takeOff:takeOffOptions];

    // Set the icon badge to zero on startup (optional)
    [[UAPush shared] resetBadge];

    // Register for remote notfications with the UA Library. This call is required.
    [[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeSound |
                                                         UIRemoteNotificationTypeAlert)];

    // Handle any incoming incoming push notifications.
    // This will invoke `handleBackgroundNotification` on your UAPushNotificationDelegate.
    [[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
                       applicationState:application.applicationState];

    // self.tabBarController = [[UITabBarController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    KFBViewController *rootView = [[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    KFBNavControllerViewController *navController = [[KFBNavControllerViewController alloc] initWithRootViewController:rootView];
    navController.delegate = rootView;
    UIViewController *aboutUs = [[AboutUs alloc] initWithNibName:@"AboutUs" bundle:nil];
    KFBNavControllerViewController *navController1 = [[KFBNavControllerViewController alloc] initWithRootViewController:aboutUs];

    UIViewController *contactUs = [[ContactUs alloc] initWithNibName:@"ContactUs" bundle:nil];
    KFBNavControllerViewController *navController2 = [[KFBNavControllerViewController alloc] initWithRootViewController:contactUs];

    UIViewController *kyfb = [[KYFB alloc] initWithNibName:@"KYFB" bundle:nil];
    KFBNavControllerViewController *navController3 = [[KFBNavControllerViewController alloc] initWithRootViewController:kyfb];

    // UIViewController *rsfm = [[RSFM alloc] initWithNibName:@"RSFM" bundle:nil];
    // KFBNavControllerViewController *navController4 = [[KFBNavControllerViewController alloc] initWithRootViewController:rsfm];

    // UIViewController *li = [[LegislatorInfo alloc] initWithNibName:@"LegislatorInfo" bundle:nil];
    // KFBNavControllerViewController *navController5 = [[KFBNavControllerViewController alloc] initWithRootViewController:li];

    // UIViewController *events = [[Events alloc] initWithNibName:@"Events" bundle:nil];
    // KFBNavControllerViewController *navController6 = [[KFBNavControllerViewController alloc] initWithRootViewController:events];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //self.viewController = [[KFBViewController alloc] initWithNibName:@"KFBViewController" bundle:nil];
    //self.window.rootViewController = self.viewController;
    self.tabBarController = [[KFBTabBarViewController alloc] init];
    self.tabBarController.viewControllers = @[navController, navController1, navController2, navController3];
    // self.tabBarController.customizableViewControllers = nil;

    self.window.rootViewController = self.tabBarController;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    // If application is launched due to  notification,present another view controller.
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
    {
        ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc] initWithStyle:UITableViewStylePlain];
        WebViewController *wvc = [[WebViewController alloc]init];
        [actionAlerts setWebViewController:wvc];
        KFBNavControllerViewController *navController7 = [[KFBNavControllerViewController alloc] initWithRootViewController:actionAlerts];
        actionAlerts.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ] initWithTitle:@"Dismiss" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss:)];
        [self.window.rootViewController presentViewController:navController7 animated:NO completion:nil];
    }

    return YES;
}



- (IBAction)dismiss
{
    [self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

}

You're presenting this controller from the window's root view controller which is self.tabBarController, but dismissing it from a navigation controller. Try this:

[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

After Edit:

I see the problem now -- your bar button item's action method is dismiss: (with a colon) whereas you implemented dismiss with no colon. They need to be the same.

通过Touch Up Inside Function通过pe和UiButton调用它

[self dismissModalViewControllerAnimated:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM