I am using Master-Detail template for ipad. I have a ViewController, which I want to show modally so I have used this code
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
m_ViewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
m_ViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[appDelegate.splitViewController presentModalViewController:m_ViewController animated:YES];
This works fine and the ViewController is loaded modally, Now I tried to dismiss this ViewController, So inside ViewController.m, I called this line of code
[self dismissModalViewControllerAnimated:YES];
This code also works fine and the ViewController gets dismissed, But after dismissing I want to call a function in my MasterView. How to do that?
Code added according to the discussion with Moxy.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.testViewController testfunction:testImage];
As amit3117 pointed out, you should use a delegate.
The protocol should be defined at least with a method that would communicate to the delegate that the view controller that was presented modally did finish its work.
#class ViewController;
#protocol MyViewControllerDelegate <NSObject>
-(void)viewControllerDidFinish:(ViewController *)sender;
#end
EDIT : I forgot to add that you should a public property for the delegate to ViewController
#interface ViewController : UIViewController
#property (nonatomic, weak) id <MyViewControllerDelegate> delegate;
#end
You could use your master view controller as the delegate. So in your master view controller implementation you would also have :
#interface MyMasterViewController () <MyViewControllerDelegate>
#end
#implementation MyMasterViewController
-(void)showViewController
{
m_ViewController = [[ViewController alloc] initWithNibName:#"ViewController"
bundle:nil];
m_ViewController.modalPresentationStyle = UIModalPresentationFormSheet;
m_ViewController.delegate = self;
// –presentModalViewController:animated: is deprecated!
[self.parentViewController presentViewController:m_ViewController
animated:YES
completion:nil];
}
-(void)viewControllerDidFinish:(ViewController *)sender
{
// Add any code you want to execute before dismissing the modal view controller
// –dismissModalViewController:animated: is deprecated!
[self.parentViewController dismissViewControllerAnimated:YES
completion:^{
// code you want to execute after dismissing the modal view controller
}];
}
#end
When m_ViewController finishes its work, it should call :
[self.delegate viewControllerDidFinish:self];
Related
My first view in the viewDidLoad has the following code
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Dialpad" bundle:nil];
self.vc = [sb instantiateViewControllerWithIdentifier:#"DialpadBoard"];
self.vc.delegate = self;
The header file contains the definition of the view controller
#property (nonatomic, retain) DialpadTableViewController *vc;
after catching an event the view loads a new modal view
- (void)handleEvent:(UIGestureRecognizer*)recognizer {
[self presentViewController:self.vc animated:YES completion:NULL];
}
The view also contains the method to dismiss the modal view:
- (void) dialpadControllerDidCancel:(SearchDialpadTableViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}
The last method never gets called.
The problem is that the modal view when it is loaded has nil self.delegate. The new modal view is loaded from the storyboard as seen below. Why the delegate is nil? I cannot how a segue since the view is in another storyboard.
you can try this
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Dialpad" bundle:nil];
UINavigationController *nav= [sb instantiateViewControllerWithIdentifier:#"DialpadBoard"];
((SearchDialpadTableViewController *)[nav viewControllers][0]).delegate = self;
[self presentViewController:nav animated:YES completion:NULL];
Since we have the nav let's take the first controller and assign the delegate
the trick ;)
((SearchDialpadTableViewController *)[nav viewControllers][0]).delegate = self;
Does SearchDialpadTableViewController define a delegate protocol and does it have a property that is specific to that protocol?
The header for that file should look something like:
#protocol ViewControllerDelegateProtocol <NSObject>
- (void)dialpadControllerDidCancel:(SearchDialpadTableViewController *)controller;
#end
#interface ViewController : UIViewController
#property (weak, nonatomic) id<ViewControllerDelegateProtocol> searchPadDelegate;
#end
Then when the user taps Done which is I'm assuming what you're trying to do (send a delegate message when that happens). You would write something like
- (IBAction)donePressed:(id)sender
{
if ([self.searchPadDelegate respondsToSelector:#selector(dialpadControllerDidCancel)]) {
[self.searchPadDelegate dialpadControllerDidCancel:self];
}
}
you must be certain, that self.vc is a class, that you expected. It can be also NavigationController
I have the following view controllers stack.
First, my app will show an app tour page. (Say TourViewController - super class is UIViewController). Added this controller in AppDelegate as rootviewcontroller.
self.window.rootViewController = tourViewController;
Then from the tour page, if the user taps on "Signin" button, I'm presenting the second view controller (Say LoginViewController - super class is UIViewController).
UINavigationController *loginNavigationController = [[UINavigationController alloc] initWithRootViewController:self.loginViewController];
[self presentViewController:loginNavigationController animated:YES completion:nil];
After a successful login, I need to resign the second view controller (LoginViewController) and want to show a tab bar based view for further needs.
I tried this code inside the login success method.
[self dismissViewControllerAnimated:YES completion:^{
TabBarViewController *tabController = [[TabBarViewController alloc] init];
[self presentViewController:tabController animated:NO completion:nil];
AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
applicationDelegate.window.rootViewController = tabController;
}];
Problems:
When I'm in the LoginViewController, I have two view controllers in my stack. So even I resign the LoginViewController, the another one (TourViewController) remains in the screen.
If I tried the above code, tab bat controller was successfully added as root view controller. But, when the LoginViewController resigns, the background was filled by TourViewController
What I need is, When I resign the LoginViewController, the background view should be tab bar controller instead of TourViewController.
Help needed!!
u can change the root view controller in AppDelegate not in the success method of the loginNavigationController better u can do this way
in AppDelegate.h
#import <UIKit/UIKit.h>
#import "TabControllerViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
- (void)showTabController; //add this method call from on success method of log in completion
#end
in AppDelegate.m
- (void)showTabController;
{
TabControllerViewController *tabController = [[TabControllerViewController alloc] initWithNibName:#"TabControllerViewController" bundle:nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
}
and in loginNavigationController.m
[self dismissViewControllerAnimated:YES completion:^{
//TabBarViewController *tabController = [[TabBarViewController alloc] init];
// [self presentViewController:tabController animated:NO completion:nil]; //no nee to present
AppDelegate *applicationDelegate = [[UIApplication sharedApplication] delegate];
[applicationDelegate showTabController]; //there is no need to create a tab bar in loginview controller, create it in root view controller
//applicationDelegate.window.rootViewController = tabController;
}];
NOTE: above is not tested just try it once
Edit 1
one way u can do it but with different animation,
form this answer u can change to second window by doing some animation for example
in in AppDelegate.h
#import <UIKit/UIKit.h>
#import "TabViewController.h"
#import "LoginViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window; //holds initial window, holds tour and login controller
#property (strong, nonatomic) UIWindow *tabWindow; //holds only tab controller
//..other code below is my test
#property (strong, nonatomic) TabViewController *tabViewController;
#property (strong, nonatomic) LoginViewController *loginController;
- (void)showTabController;
#end
in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_tabWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
// Override point for customization after application launch.
_loginController = [[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil];
_tabViewController = [[TabViewController alloc] initWithNibName:#"TabViewController" bundle:nil];
self.window.rootViewController = _loginController; //for test for your case it contains tour view controller
[self.window makeKeyAndVisible];
return YES;
}
- (void)showTabController;
{
[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
self.window.rootViewController = _tabViewController;
} completion:^(BOOL finished) {
// [_tabWindow makeKeyAndVisible];
}];
}
Do one thing,
Create UINavigationController in AppDelegate.h so you can access it anywhere.
Logic
Whenever you need to change navigation controller you must have to put your Controller to Navigation stack.
So first of all you have to create ViewController/ Tabbarcontroller object and assign it to navigationController and then show the navigationController.
AppDelegate* myDelegate = (((AppDelegate*) [UIApplication sharedApplication].delegate));
InitialViewController *initialVC = [self.storyboard instantiateViewControllerWithIdentifier:#“InitialVC"];
myDelegate.navController = [[UINavigationController alloc] initWithRootViewController:initialVC];
myDelegate.window.rootViewController = myDelegate.navController;
[myDelegate.window makeKeyAndVisible];
line presentViewController crash app
#interface VVPassbook : NSObject
#property (nonatomic, retain) id delegate;
-(void)gotPassbookFromUrl:(NSURL *)passbookUrl;
#end
#import "VVPassbook.h"
#implementation VVPassbook
-(void)gotPassbookFromUrl:(NSURL *)passbookUrl
{
//present view controller to add the pass to the library
PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
[vc setDelegate:(id)self.delegate];
[self.delegate presentViewController:vc animated:YES completion:nil];
}
#end
im call this method in AppDelegate.m in method
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
VVPassbook *pass = [[VVPassbook alloc] init];
pass.delegate = self.window.rootViewController;
[pass gotPassbookFromUrl:MYURL ];
}
all times error
reason: 'Application tried to present a nil modal view controller on target <VVRightManuViewController: 0x15dd28460>.'
you should present a viewController from a UIViewController, i.e., self
(if self is a viewcontroller).
Note that you will get a "Attempt to present on whose view is not in the window hierarchy"-warning when self (this viewcontroller) is detached from the Main Window.
Solution: always use addChildViewController: to child viewcontrollers.
Why are you using self.delegate to push the view Controller,
simple write
[self presentViewController:vc animated:YES completion:nil];
in the place of
[self.delegate presentViewController:vc animated:YES completion:nil];
I've been receiving a warning that is related to my use of the GKTurnBasedMatchmakerViewController and the BannerViewController for iAd. That warning is:
Presenting view controllers on detached view controllers is discouraged <RootViewController: 0x14cd143c0>
What is wrong with the sequence of code shown below that causes this warning?
In AppDelegate.h
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *viewController;
}
In AppDelegate.m
- (void) applicationDidFinishLaunching:(UIApplication*)application {
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window setRootViewController:viewController];
[window addSubview: viewController.view];
}
Then when the user presses a Play button on my home screen, I first open the game center view controller as follows (notice the 2nd line is setting presentingViewController equal to the rootViewController that was set up in the appDelegate):
AppDelegate * theAppDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
self.presentingViewController = theAppDelegate.viewController;
GKTurnBasedMatchmakerViewController *mmvc = [[GKTurnBasedMatchmakerViewController alloc] initWithMatchRequest:request];
[presentingViewController presentViewController: mmvc animated: YES completion:nil];
After the above code runs, a new scene is loaded. This scene is described in my GameSelectionLayer.h as follows:
#interface GameSelectionLayer : CCLayer <InAppStoreControlLayerDelegate> {
...
RootViewController *viewController;
AppDelegate *app;
BannerViewController *bannerViewController;
}
Then in my GameSelectionLayer.mm, I load the bannerViewController onEnter as follows:
-(void)onEnter {
[super onEnter];
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
viewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] viewController];
bannerViewController = [[BannerViewController alloc] initWithContentViewController:viewController];
app.window.rootViewController = bannerViewController;
}
I receive the above mentioned warning when the above code runs. Please let me know what you think I might be doing wrong that is causing this warning.
The problem is you are replacing the view hierarchy of a view controller which has another view controller presented. The "detached" warning comes when you try to present a view controller from a view controller which is not attached to a window, or does not have a descendent view controller attached to a window.
Here I recommend two approaches. Either dismiss all view controllers before replacing the root view controller, or, the better of the two options, use another window with its own root view controller hierarchy.
Also, note that you should not add the view controller's view as a subview of the window. The system does it for you when you set the view controller as the root view controller.
I'm normally just use storyboard for my iOS, but needed to integrate an SDK which does not use storyboard, rather xib. My initial view controller is from storyboard, and when a button is pushed (IBAction), I would like it to go to the xib view controller, but not sure how programmatically to do so. Here is my AppDelegate:
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:...{
// set to storyboard on launch
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"iPhoneStoryboard"
bundle: nil];
UIViewController* viewController = [mainStoryboard instantiateInitialViewController];
[self.window setRootViewController:viewController];
[window makeKeyAndVisible];
return YES;
}
Here is my code .h:
//mainVC.h
#import <UIKit/UIKit.h>
#interface MainVC : UIViewController{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *buttonScan;
-(IBAction) ActionScan;
#end
And the .m:
//mainVC.m
#interface MainVC ()
#end
#implementation MainVC
#synthesize window;
#synthesize navigationController;
#synthesize buttonScan;
- (IBAction)ActionScan{
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
create instance of your xib viewController with initWithNibName
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
& then push this controller instance on navigation controller
[self.navigationController pushViewController:desController animated:YES]
In the view controller in storyboard, code below:
alloc & init destination view controller:
UIViewController *desController = [[SomeViewController alloc]init];
show the destination view controller:
[self.navigationController pushViewController:desController animated:YES]
or
[self.navigationController presentViewController:desController animated:YES completion:nil
Usually you don't set the window's rootViewController directly. Instead you should use
- (IBAction)ActionScan{
[self presentViewController:self.navigationController animated:YES completion:nil];
}
to show your next view controller. The first and accepted answer to this question gives a more detailed explanation of how this works.
Changing root view controller of a iOS Window
Setting window.rootViewController and calling [window makeKeyAndVisible] usually happens in your app delegate, but since you're using storyboards, it is done for you under the hood.
Also, unless you're using multiple windows, you can access your UIWindow from anywhere using your app delegate singleton object.
MyAppDelegateClass *appDelegate = (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate];
UIWindow *mainWindow = appDelegate.window;