Manually Instantiate ViewController and push on top. Good practice? - ios

What i'm doing is this:
UIViewController *rootController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
AlarmRingViewController *alarmController = [[AlarmRingViewController alloc] init];
[rootController presentViewController:alarmController animated:YES];
What i want to achive with this, is to push my AlarmRingViewController on top of any other controller which is displaying at the moment and it works so far.
Now im wondering if this is good practice:
to instatiate a new viewController each time it should be presented
do so in a non UI related class? (in my case a scheduler for NSTimer)
from there push the newly created viewController with the rootViewController on top
Or does this violate the MVC pattern or Apples guidelines or anything.
cheers

Personally I think it's ugly code. It's hard to read and hard to debug. Split the code up a bit:
UIViewController *rootController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
AlarmRingViewController *alarmController = [[AlarmRingViewController alloc] init];
[rootController presentViewController:alarmController animated:YES];
There is no benefit to typing so much into one line.
Update: Based on your updated question:
There is no problem instantiating a new view controller each time you need it. This is very common. It might be appropriate to create one and cache it. This is an optimization that could make sense if only one of the view controllers is every shown at any given time, the view controller is used very often, and it takes a lot of time to create.
View controllers are usually created and presented by other (view) controllers.

Why don't you just use a UINavigationController as the root view controller. That way, you can just do this:
AlarmRingViewController *alarmController = [[AlarmRingViewController alloc] init];
[self.navigationController presentViewController:alarmController animated:YES completion:nil];

Related

What is best practice when NOT using storyboards or nibs?

I've heard Facebook and Google do not use UIStoryboards or nibs because they are difficult to merge – they format all their views programmatically. Are there any resources out there that can provide some guidance as to how best to position assets, handle localization, organize files, etc., when creating all your views without nibs?
The first step would be to create an UINavigationController or a UITabBarController within the AppDelegate's didFinishLaunching method, where you should set the rootViewController for the current UIWindow ([window setRootViewController:]).
Then you just have to create your content viewcontrollers and views. Let's say you want to create a menu, then you should create a MneuViewController which inherits from UIViewController and a MenuView which inherits from UIView. Within the MenuView code you create your view components like labels, textfields or whatever you need. In the MenuViewController you create an instance of the MenuView class and call [self setView:] with that object. Finally you have to add the ViewControllers to your rootViewController.
The AppDelegate should look similar to something like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MenuViewController *mvc = [[MenuViewController alloc] init];
UINavigationController *rootViewController = [[UINavigationController alloc] initWithRootViewController: mvc];
[self.window setRootViewController: rootViewController];
[self.window makeKeyAndVisible];
}
Localization has to be done using the macro NSLocalizedString() for which you should find a lot of samples.
Files should be handled using NSFileManager.
For eveything else you should ask more specific questions.

Presenting modalViewController is dealloc'ing the one calling it

I'm having a few problems integrating Greystripe adverts (documented here but not important). A way around my problem if to just present my gameView like this
iSlideAppDelegate *appDelegate = (iSlideAppDelegate *)[[UIApplication sharedApplication] delegate];
UIStoryboard *storyboard = self.storyboard;
Game3ViewController *gameView = (Game3ViewController *)[storyboard instantiateViewControllerWithIdentifier:#"mainGameController"];
[appDelegate.window addSubview:gameView.view];
This is basically just adding my gameView on top of the current view controller.
Now from this view I want to show another modal view like this
FullScreenSelfAdViewController *adView = [[FullScreenSelfAdViewController alloc] initWithNibName:#"FullScreenSelfAdViewController" bundle:nil];
[adView setDismissDelegate:self];
[self presentModalViewController:adView animated:YES];
The problem is, when this view is displayed, my gameView deallocates. Meaning when I call to dismiss the AdView it dealloc's but stays on screen as the underlying gameView isn't there any more.
Is there a better way to call these views? (I can't call my gameView by presenting it as a modal view).
Or a way to keep the gameView from deallocating?
If I had to guess, you don't have a strong variable retaining the class that gets dealloced. Whoever creates that class should have a strong ivar doing so. I had this exact same problem when I tried to create an object and use it without retaining it. Note that for UIAlerts, the system retains it when you call [alert show], leading to think you can do this with other view controllers (which you cannot!)
Good luck.

Xcode push and pop view to save data

I am trying to log data from the FirstView (one text field and one button) to SecondView (UiTableView) for example contacts list. I would like to briefly explain my code.
I have added a NavigationController with TabBar in the AppDelegate.m;
UINavigationController *pointer = [[UINavigationController alloc] init];
self.window.rootViewController = pointer;
[pointer setNavigationBarHidden:NO];
[pointer pushViewController:self.viewController animated:YES];
And I have used a [self.navigationController pushViewController:push animated:YES]; to push the data to the second screen but it always go to another NavigationController view as loop with BACK button, then I have used [self.navigationController popToRootViewControllerAnimated:YES]; which it does not pass the data to the another view. Unless I close the program completely an open it back again. So is there a way to push and pop at the same time? Or could you please show me another way?
Thanks in advance.
Note I have shared my code in this link;
Mycode
You can try to create a singleton class that manages this data. Then you just call the same singleton class throughout your code, it should keep the same state throughout the lifetime of your app.
Or, a simpler way is to Set up "myArray" as a property in your App Delegate.. Then, in your class' implementation file, do something like this:
UIApplicationDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.myArray = newArray;
this assumes myArray and newArray has been properly initialized.

pushViewController does not cause new controller to draw view

Preface: I am not using *.xib files.
I instantiate a UINavigationController in a class that effectively serves as my 'rootViewController'. This 'rootViewController' also has two UITableViewController members that are drawn on different sections of the iPad screen. One of which is set as the root view for the navigation controller. Let's call it tableViewControllerA.
The problem is, when I invoke pushViewController on a valid UINavigationController, I see no effect:
[tableViewControllerA.navigationController pushViewController:tableViewControllerX animated:YES];
I've gathered from the posts I've searched today, that this push method should in turn cause the screen to redraw the top of stack controller.view. This is not what I'm seeing.
It seemed there was a disconnect in my implementation, and it was time to reference a working example in my environment (xcode 4.0). Assuming the canned templates would provide a working basis, I created a new navigation-based applications. I simply modified didFinishLaunchingWithOptions: as follows.
UIViewController *view1 = [[UIViewController alloc] init];
UIViewController *view2 = [[UIViewController alloc] init];
view1.title = #"view1";
view2.title = #"view2";
[self.navigationController pushViewController:view1 animated:YES];
[self.navigationController pushViewController:view2 animated:YES];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:view1];
[view1 release];
[view2 release];
I found similar results. When I launch the simulator the screen title reads the title of whatever the self.window.rootViewController is pointing at. With the code as is, the title of the resulting top screen reads "view1". When I initWithRootViewController:view2, the resulting top screen reads "view2".
So please tell me I'm stupid cuz xyz...
Thanks.
Here are some references and suggestions:
Simple tutorial for navigation based application:
http://humblecoder.blogspot.com/2009/04/iphone-tutorial-navigation-controller.html
Here is another one to create the step by step navigation controller and adding the views:
http://www.icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/
and here a bit advance with navigation + tab bar controller:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CombiningToolbarandNavigationControllers/CombiningToolbarandNavigationControllers.html
Without seeing your code, I have 2 theories:
Your syntax and calls are wrong when you do the push. Use this as a model:
-(void)Examplemethod {
AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:#"AnotherClassView" bundle:nil] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
}
You are never adding the navigation controller to the view hierarchy which never adds the view either. Take a look at this.

How to use a UISplitViewController as a Modal View Controller?

I am trying to display a UISplitViewController presenting it as a Modal View Controller in my iPad app. I manage to have it display, but for some reason there is a gap to the left of the modal view the size of a Status Bar which is also preserved when the orientation is changed.
Does anybody know why this is happening? Or if this is even possible? Maybe I'm just digging myself a huge hole.
Like for many of you, I needed a 'modal way' to use the UISplitViewController. This seems to be an old issue, but all I found in StackOverflow was at best an explanation why the problem happens when you attempt to do so (like the accepted answer above), or 'hack-arounds'.
However, sometimes it is also not very convenient to change much of your code-base and make a UISplitViewController the initial object just to get it's functionality up and running.
In turns out, there's a way to make everybody happy (including Apple guidelines). The solution that I found best, was to use the UISplitViewController normally, but when needed to be shown/dismissed, use the following approach:
-(void)presentWithMasterViewController: (UIViewController *) thisMasterViewController
andDetailViewController: (UIViewController *) thisDetailViewController
completion:(void(^)(void))completion
{
masterViewController = thisMasterViewController;
detailViewController = thisDetailViewController;
[self setViewControllers:[NSArray arrayWithObjects:masterViewController, detailViewController, nil]];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.window.rootViewController = self;
[self.window makeKeyAndVisible];
if(completion)
completion();
}
-(void)dismissViewControllerWithCompletion:(void (^)(void))completion
{
self.window = nil;
masterViewController = nil;
detailViewController = nil;
if(completion)
completion();
}
Where "window", is a property of your UISplitViewController subclass. And the system will take care of the rest!
For convenience/reference, I uploaded this as a UISplitViewController subclass to gitHub:
ModalSplitViewController
--EXAMPLE ON HOW TO USE --
mySplitViewController = [[ModalSplitViewController alloc] init];
mySplitViewController.delegate = self;
[mySplitViewController presentWithMasterViewController:masterViewController andDetailViewController:detailViewController completion:nil];
// when done:
[mySplitViewController dismissViewControllerWithCompletion:nil];
mySplitViewController = nil;
Side-note: I guess most of the confusion originates from the fact that
the UISplitView usage example from Apple documentation uses the window
created in the appDelegate, and for the fact that most people are not
so familiar with the window concept - because we normally don't need
to (they are created once in StoryBoards or boilerplate code).
Additionally, if you are doing state restoration, one should not
forget that programmatically-created UIViewControllers won't
automatically be restored by the system.
The stock UISplitViewController was designed for use as the root view controller only. Presenting one modally goes against the Apple Human Interface Guidelines and has a high probability of getting rejected by the App Review Team. In addition, you may receive the error:
Application tried to present Split View Controllers modally
Technically, this is what I did:
1/ Subclass a UIViewController ie. #interface aVC: UIViewController
2/ In the viewDidLoad, set up a splitViewController, ie. aSplitVC
3/ Then self.view = aSplitVC.view
After all, present aVC as modalViewController
I agree with Evan that this is slightly off-color for Apple, but I was able to complete a working version of this with the following solution:
UISplitViewController *splitVC = [[UISplitViewController alloc] init];
splitVC.delegate = VC2;
splitVC.viewControllers = [NSArray arrayWithObjects:navcon1, navcon2, nil];
UINavigationController *splitNavCon = [[UINavigationController alloc] init];
splitNavCon.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[splitNavCon.view addSubview:splitVC.view];
VC2.splitParentViewController = splitNavCon;
[self presentViewController:splitNavCon animated:YES completion:nil];
This allowed me to have a working back button in the new UISplitViewController that was presented modally on the screen.
You'll notice that I actually pass the VC2 (the delegate of the UISplitViewController) its parent UINavigationController. This was the best way that I found I could dismiss the UISplitViewController from within the VC2:
[splitParentViewController dismissViewControllerAnimated:YES completion:nil];
I believe one can do the other way around: instead of custom controller presenting split controller, one can set up the split controller as the root window controller in storyboard, and on top of its view you can add your custom controller (ie, login screen) and remove it from the screen (removeFromSuperview for example) when it is needed.
That answer is not actually correct, because it not valid any longer since iOS8 and if you need to support even iOS7 you can do that like you put actually modally UIViewController which has a container as SplitView.
let mdSplitView = self.storyboard?.instantiateViewControllerWithIdentifier("myDataSplitView") as! MyData_SplitVC
self.addChildViewController(mdSplitView)
mdSplitView.view.bounds = self.view.bounds
self.view.addSubview(mdSplitView.view)
mdSplitView.didMoveToParentViewController(self)

Resources