Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call presentModalViewController from NSObject class

In my subclass of NSObject I would like to call something like

    [[self navController] presentModalViewController:myView animated:YES];

But none of my tries were successful. How can I call a modal view if I'm not in a subclass of UIViewController?

Solution:

#import "myProjectNameAppDelegate.h"
// ...
MyViewController *myView = [[MyViewController alloc] init];
myProjectNameAppDelegate *appDelegate = (myProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate navController] presentModalViewController:myView animated:YES];
like image 806
testing Avatar asked Sep 21 '10 12:09

testing


1 Answers

better way to call a presentModalViewController is, passing viewcontroller to the NSobject class. call the nsobject function from the uiviewcontroller Here is the code with mail example

In view Controller //your current view

[nsobjectclassObject OpenMailComposer:self]; //this will take the viewcontroller to NSobject class

In NSObject class //may be sharing class

-(void)OpenMailComposer:(UIViewController*)view
{

    viewControllertoShow = view; // viewControllertoShow is UIVIewcontroller object
    MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc]init];
    mailView.mailComposeDelegate = self;
    [mailView setSubject:@"Hey! check this out!"];
  [viewControllertoShow presentModalViewController:mailView animated:YES];
}

For dismissing from NSObject class you can do the following

[viewControllertoShow dismissViewControllerAnimated:YES]

like image 122
name-it Avatar answered Oct 04 '22 15:10

name-it