Trying to Get Back to the First UIViewController of the App - iphone

I have a app that I am trying to reload the original viewcontroller from the MainViewController. There is no .xib the first view of the app is created in the AppDelegate. After I have loaded a couple UIViewControllers that did other things, I need to return to the original webView (ViewController) that was launched by the app on startup. I am really having a hard time getting anything to work that researched on the web. I just need a way to reload the original view that the app started with. Can anyone provide any possible solutions for me to try please? I am running out of options to try.
Thank!

You can set the rootViewController again.
In the app delegate use:
self.window.rootViewController = yourFirstViewController;
Good Luck!
EDIT:
Also, you can access to the app delegate globally using:
[[[UIApplication sharedApplication] delegate]
I published a little example project that will help you:
https://github.com/luisespinoza/ChangeViewController
Good Luck!

Related

Switch between view without closing view xcode

I have a Login interface, user interface, and a Main menu.
When I'm logging in, I switch on my user interface.
if I do a UIModalTransition, and if I come back to the page, I need to RE-login again, it's a problem for me.
I search a method for Switch between view but don't close them, because if you closed them, views restart, it's the same for UIWebview, I would like to switch between views without closed and reset views.
Is it possible ?
I don't really know what kind of Code is it.
If you have ideas, please tell me.
In this answer i tried to explain the view controllers orchestration as i see it.
The idea is to create the manager class taking care of elements storage and presentation. It also mentions the way to manage the login information.
You can use pushViewController ie..
[self.navigationController pushViewController:yourUserInterFaceViewController animated:YES]; for transition between login and user interface.
you can use 3 ways
1)pushing the view into UINavigationController
2)presenting the view with ModalViewController
3)adding subView to rootViewController and remove it using removeFromSuperView when needed.
(you can allocate them when app launches and access them using
[YourAppDelegate sharedApplication].yourViewController)
Since your appDelegate is a singleTon class it will always return the same viewcontroller you've allocated at the time of launching the app. You can use Lazy Load also.
May be this will help.

Linking One View To Another Via A Button

I'm a bit new to programming for iOS, and I'm having some trouble linking one view to another via a button. I'm just creating a simple little app that does some calculations on a NSDate in an attempt to learn XCode and iOS programming.
I've already searched this quite a bit, and I've tried to learn from other examples but I'm having trouble getting the view to present, nothing happens when I press my button (which I've already checked to be linked to the button).
I've been having trouble understanding view programming, so please bear with me.
Here's my code for my button:
-(IBAction)resultsPressed
{
TimeResults *timeResults;
timeResults = [[TimeResults alloc] initWithNibName:#"TimeResults" bundle:nil];
[self.navigationController pushViewController:timeResults animated:YES];
[timeResults release];
}
TimeResults.xib is using a Navigation Controller if it matters, while my root view is simply a view. My thinking behind this was so that I could get the "back" button (though I'm not sure if this is the correct way tot do this, since they are not a part of the same hierarchy). Any suggestions on how this should be done would be greatly appreciated!
Nothing seems wrong with the code you posted, but you should have the Navigation Controller associated with the first nib, as the back button will display by default when a new view is pushed onto the stack.
Also, make sure that the Navigation Controller is set up properly in your AppDelegate. The proper way to do this can be seen if you start a new project and select "Navigation-based Application". If you use the new project as a sample to show you how to set up your old project correctly, you will have to make sure that the nib is set up correctly too. I would suggest using the new project, hooking it up as a UIViewController instead of a UITableViewController, and then moving your code from your old project to this new one.
Finally, make sure that you always import the .h file of the UIViewController you are going to push to. Hope that helps!
To load another view, try
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
TimeResults *timeResults = [[TimeResults alloc] initWithNibName:#"TimeResults" bundle:nil];
[delegate.window setRootViewController:timeResults];
Hope it works. :)

How to implement Ads in an iPhone Application so as to avoid reloading of ads in all the views?

Ok, i've this multi view IPhone application in which i've integrated adWhirl ads. That's the good news. The bad news is that i've literally implemented the adWhirl ads in each view controller, which means every time a view is pushed a new adWhirl object is created which takes time to load the AdMob/iAds advertisements. Now, I can't help thinking about this being the "wrong" way to go about the problem. So the question is, do I make a singleton ad object maybe in the appDelegate's didFinishLaunching method and use that ad object in all the view controllers or what I've done is pretty much how it should be done?
Put the ad in the appDelegate and reference or sub class it from all of the view controllers. I just finished a Tab Controller app that had a similar requirement. The performance gain was a nice bonus as well.
You could probably just add it to the window in the appDelegate.
[self.window addSubview:same_as_before.view];
[self.window addSubview:ad.view];
[self.window makeKeyAndVisible];
This way it will always be in the same spot on top of all other views and you won't have to deal with it again.

Buttons make my multiview iphone app crash! (but single view is okay)

This is probably some rookie mistake, but I can't figure it out. I've established a button within my app to recall a link in safari. From my method file it looks like this:
Obj C Code:
-(IBAction)linkSafari{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://en.wikipedia.org/"]];
}
I linked it up in IB and all seems okay.
When I create this same setup as a single view app it works great, but whenever I click it in my multiview app it's an automatic crash. Acts the same way on both my simulator and physical ipod.
Is there an endless list of places I could have screwed up or is there a certain area I should look into?
I don't think the problem is in that particular function,
but on the way you create the views and viewcontrollers
For anyone interested, this was solved thanks to an extra set of eyes. On my tab bar, I'd declared the assigned nibs for every section, but missed a few class associations. That's what messed me up.

iPhone DropBox App Like implementation - flipping tab bar to reveal login screen

I am writing an application where you need to show login screen modally and the app has a tab bar.
I have added tab bar directly to the UIWindow. To flip it to a new view (login view) I have overridden applicationDidFinishLaunching where I check if user has login credentials, then I do not show the login screen otherwise (assuming first time use or logout case) I modally present the login screen. I have given an option of logout in a settings tab inside the app.
I am using [[UIApplication sharedApplication] delegate] call to get instance of app delegate when user logs in first time. This way I get access to the tabBarController that is part of the Application Delegate (as is most of the times). However, when I try to call my loginViewController from the logout option in settings (somewhere in future life cycle), the same call [[UIApplication sharedApplication] delegate] returns me a delegate on which I am not able to use any of the methods I have defined. It gives me "unrecognized selector sent to instance" error at runtime.
I need to understand what exactly the call [[UIApplication sharedApplication] delegate] returns? Does the delegate object it returns change over the period of application life cycle? OR is it a singleton instance through out the app life cycle?
And secondly to resolve this, should I add the tabBar to a view (contained in main window) instead of adding it directly to the UIWindow (as done by the template for Tab Bar application and seems to be the standard practice). Are there any known problems with this approach OR its okay to do so. Any one has tried this? Please let me know.
Thanks
Dev.
It sounds like your class that gets an instance of your singleton delegate doesn't know what it implements. make sure you are #importing your delegate to the class that uses it as [[UIApplication sharedApplication] delegate]. Also, if you get a warning about UIApplication not conforming or whatever, you can cast it to your AppDelegate type to avoid it.
To answer your question about what this call returns, it is a singleton throughout the lifecycle of the app.
To answer the 2nd question, having it in the UIWindow (and thus in the appdelegate) is fine, and probably encouraged, since it is the root controller of your app (from the sound of things)