Call presentModalViewController from NSObject class - iphone

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];

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]

I don't see a way to display a modal view without a ViewController. You have to store a reference to a UIViewController in your class so you can access it. Or setup a property in your AppDelegate, which you can get by calling [[UIApplication sharedApplication] delegate];

If you hold the navigationController or some viewController, you can present a modal view controller.
What is your myView? Is it a view, is it a viewController. I hope that it is a viewcontroller otherwise, this is the reason your code doesn't run

Related

Not able to get Current ViewController inside applicationWillResignActive

I have a storyboard based Single View App;
I have 3 ViewControllers on my Storyboard linked to 3 ViewController classes in the code;
I browse between ViewControllers by doing this:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
MenuViewController* mainMenu = [sb instantiateViewControllerWithIdentifier:#"vcMainMenu"];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainMenu animated:YES];
Now I need to access different UIcontrols on the applicationWillResignActive from the current active ViewController, I will access different controls depending of the ViewController, I'm trying to accomplish this by doing:
if ([self.window.rootViewController isKindOfClass:[LowProfileViewController class]])
{
NSLog(#"here!");
}
But it always returns the rootViewController. How Can I get the current displayed rootViewController from applicationWillResignActive?
Please, Incorporate NavigationController is not an option...
thanks
You could try this -
Make An AppDelegate #property (i.e. currentModelViewController)
#property (nonatomic, retain) UIViewController *currentModelViewController;
For each ViewControllers in viewDidAppear or in viewWillAppear
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.currentModelViewController = self;
Now this will work
if ([self.currentModelViewController isKindOfClass:[LowProfileViewController class]])
{
NSLog(#"here!");
}

How to change first simple singleview viewController to navigation viewController?

I first like to load simple viewController which shows some option and then clicking on some button I would like to load navigationController or tabbarController depending on button click. How can I do this ?
I replace the root view controller on the window when I want to switch the views.
For example in my app I show a loading screen first then I switch the view to a login screen.
To do this you need a reference to your app delegate then you can access the window property and replace the root view controller:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
LoginViewController *loginVC = [[LoginViewController alloc] init];
appDelegate.window.rootViewController = loginVC;
In your simpleViewController :
- (IBAction) yourButtonAction:(id)sender
{
UIViewController *Vc = [[theViewControllerYouWantToShow alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc];
[self presentModalViewController:nav animated:YES];
}
Edit :
you have three options to show your viewController content :
as the example above using presentModalViewController:
add the viewController view as a subView to the current viewController.
in your case : [simpleViewController.view addSubView:nav.view];
3.or if your simple ViewController is the navigation root viewController you can push other viewControllers to its navigation stack.
in appdelegate.h
#property (strong, nonatomic) id<UIApplicationDelegate>delegate;
in appdelgate.m
#synthesize delegate;
in my first viewController's .h file
AppDelegate *myappDelegate;
-(IBAction)start:(id)sender;
in my first viewController's .m file
-(IBAction)start:(id)sender
{
NSLog(#"Start Button is clicked");
mvc = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
myappDelegate = [[UIApplication sharedApplication]delegate];
myappDelegate.navigationController = [[UINavigationController alloc]initWithRootViewController:mvc];
myappDelegate.window.rootViewController = myappDelegate.navigationController;
[myappDelegate.window makeKeyAndVisible];
}

Change from UIViewController to UITableViewController

This one make me go crazy. I am building an iphone app where the first view is a login view. UIViewController, when the user succesfully logs in i want to display i table view. Somehow i just have big problems doing this.
In my app delegate i load my loginViewController, and then i want from the loginViewController load my listViewController.
What is the logic behind switching to a UITableViewController from a UIViewController?
you'd better to do it in your app delegate and surely NOT add the UITableViewController.view to the UIViewController.view... just add it to the UIWindow and then dismiss the old UIViewController (removeFromSuperView it's view and then release it)
EDIT:
that's how i manage:
i add a method in my appDelegate:
-(void)switchMainView;
and from my UIViewController i just call it with this:
[[[UIApplication sharedApplication] delegate] switchMainView];
in switchMainView i just
remove my UIViewController.view from superview,
release UIViewController,
alloc the UITableViewController and init it, then
add its view to the window app:
-(void)switchMainView{
if (mainView!=nil){ // mainView is the UIViewController
[mainView.view removeFromSuperview];
[mainView release];
mainView = nil;
}
Menu *vc; // Menu is my class, subClass of a UITableViewController
vc = [[Menu alloc] init];
nc = [[UINavigationController alloc] initWithRootViewController:vc];
[window addSubview:nc.view];
[vc release];
}
and then i do the same for going back, eventually
Assuming you already have your custom UITableViewController created:
YourTableViewController *vc = [[UITableViewController alloc] initWithStyle:...];
[self presentModalViewController:vc animated:YES];
[vc release];
you can use either i do'nt think there is a major impact but definitely they might have some advantage/Disadvantage over other..
for better understanding read the below tutorial.
http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html

How to access custom UIViewController in a UISplitViewController from another class

I have a custom customUIViewController in a UISplitViewController and want to access the instance of the customUiViewController from the detailView (which is another UIViewController inside the UISplitViewController) from another class; how can I do this?
CODE SNIP (Dont worry about the syntax; it is shorten up)
myAppDelegate.m
customViewController *masterView = [[customViewController alloc] init;
UINavigationController *NVC = [[UINavigationController alloc] initWithRootViewController:masterView];
MYViewController *detailView = [[MyViewController alloc] init;
UISplitViewController *mySplit = [...];
mySplit.viewControllers = NSArray[...masterview,detailView,nil];
[window addSubView:mySplit view];
MyViewController.m
-(void) someMethod {
customViewController *myInstance = (customViewController)[self.splitViewController objectAtIndex:0]; ??
// I think this just gets the outter UINavigationController
[myInstance doSomething];
}
customViewController.m
-(void) doSomething {
}
I want to be able to get access to customViewController to call the doSomething method. Both customViewController and myViewController is inside the same UISplitViewController
UIViewControllers have a splitViewController property so try using that to get a reference:
customViewController *myInstance =
(customViewController *)[self.splitViewController.viewControllers
objectAtIndex:0];
Index 0 is the left-side view controller in the split view controller.
Edit:
If the left-side view controller is a UINavigationController, then to get the root view controller of that, do this:
UINavigationController *nc =
(UINavigationController *)[self.splitViewController.viewControllers
objectAtIndex:0];
customViewController *myInstance =
(customViewController *)[nc.viewControllers objectAtIndex:0];
If you're working with the default UISplitView that XCode makes, you need to reference the AppDelegate to get the splitView's ivar:
YourAppDelegate *del = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
UISplitViewController *split = del.splitViewController;
NSArray *vcArray = split.viewControllers;
//left is objectAtIndex:0, right is objectAtIndex:1

Create a new ViewController object only if it doesnt exist

I use the following code to bring up a view
-(IBAction) openSomeView{
SomeView *sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
How can I detect if this view has been created already and if so, then just show it now create a new object?
Thanks
first declare in #interface
SomeView *sv;
and the you can check it
-(IBAction) openSomeView{
if(sv==nil)
sv = [[SomeView alloc]initWithNibName:#"SomeView" bundle:nil];
[self presentModalViewController:sv animated:NO];
[sv release];
}
You cannot create more than one instance using this code, since you present modally the view controller.
Else, you'd probably keep a member variable in your class and check it against nil.
EDIT: or you can implement the 'Singleton' design pattern, if that's the meaning you search.
I have a couple of heavy-weight view controllers in my app. Right now I'm handling this situation as follows:
save MyViewController to appDelegate
in "openSomeView" method I'm checking if MyViewController was already created, if no - create it:
-(IBAction) openSomeView {
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if ( !appDelegate.myViewController ) {
appDelegate.myViewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController"
bundle:nil];
}
[self presentModalViewController:appDelegate.myViewController animated:NO];
}