Linking One View To Another Via A Button - iphone

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. :)

Related

Switching view controllers without navigation controller

I'm sure this has been asked countless times, and I've seen similar questions though the answer still eludes me.
I have an application with multiple view controllers and as a good view controller does its own task. However I find myself stuck in that I can't switch from one view controller to another. I've seen many people say "use a navigation controller" but this isn't what I want to use due to the unwanted view elements that are part and parcel to view controller.
I've done the following and have had limited success. The view controller is switched but the view does not load and I get an empty view instead:
- (IBAction)showLogin:(id)sender
{
PPLoginViewController *login = [[PPLoginViewController alloc] initWithNibName:#"PPLoginViewController" bundle:nil];
PPAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = login;
[self.view insertSubview:login.view atIndex:0];
}
Using UINavigationController as a rootViewController is a good tone of creating iOS application.
As i understand unwanted view elements is a navigationBar? You can just hide it manually, setting:
[self.navigationController setNavigationBarHidden:YES];
And about your case, if you want to change you current viewController(targeting iOS 6), you can just present new one:
[self presentViewController:login animated:YES completion:nil];
or add child (Here is nice example to add and remove a child):
[self addChildViewController:login];
Why to set UINavigationController as a root?
1) First of all it makes your application visible viewcontrollers to be well structured. (Especially it is needed on iPhone). You can always get the stack and pop (or move) to any viewController you want.
2) Why I make always make navigation as a root one, because it makes the application more supportable, so to it will cost not so many code changes to add some features to the app.
If you create one (root) viewcontroller with a lot of children, or which presents other viewcontrolls, it will make your code really difficult to support, and make something like gode-object.
Listen to George, UINavigationController is the way to go. Your reasons for not wanting to use it are not valid.
However, the reason your code doesn't work might have to do with the unnecessary line after setting the rootViewController to the login vc.
Per Apple's documentation, setting rootViewController automatically sets the window's view to the view controller's view.

Trying to Get Back to the First UIViewController of the App

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!

TabBar Application with a View that has a UIButton on the View to goto another View outside the TabBar

I'm new to iPhone development, and multiple views (xib or nib) are really confusing me. This is what i'm trying to achieve...
using TabBarControllerAppDelegate
Have 5 different TabBar Items and have created 5 different Views thru the TabBarController
The First View has a UIButton that is a Next button that needs to go to another view called View2.XIB.
I setup a new UIViewController which references the View2 and an IBAction for the switchPage, etc but not able to get it to do anything when clicking on the button.
All of My TabBar buttons work but not able to Navigate to anything outside of the Tabbar
Any help in this regard will be highly appreciated. Anyone have any examples
IBAction switchPageButtonPressed:(id)sender
{
[self.tabbarcontroller.tabBar setSelectedItem:[self.tabbarcontroller.tabBar.items objectAtIndex:1]];
here 1 means ur 2nd tabbar
}
It is difficult to find the problem without the code, but I will assume your action code for the switchPage button is incorrect. You should use code similar to the following:
- IBAction switchPageButtonPressed:(id)sender
{
ViewController2 *view2VC = [[ViewController2 alloc] initWithNibName:#"View2" bundle:nil];
[self presentModalViewController:nview2VC animated:YES];
[view2VC release];
}
If you are confident your method works, then you will want to verify that the action is hooked up correctly. The easiest way to do this is to place a breakpoint on the method and run the app in Debug. When you click the button, the debugger should break on your method, if it doesn't, you will need to check your connections in Interface Builder.

Help Menu iPhone

I have a design question/technical question about my iPhone app.
I have a pretty simple (read really really simple) single view application. And it does everything that I need it to do. However I find myself in need of a help view. And I really don't quite know what to do!
I have a simple helpButton() method in my main view controller, and I really just want to display a scrollview with a bunch of images that show what to do during the use of my app. However, should I make a new viewcontroller class? How do I call it from my method?
Really I was thinking of an unfortunately simple method, just putting a scrollview behind everything and hiding it. Then showing it when the IBAction is called. Horrible...
Sorry if this is elementary, I haven't needed to do anything more yet!
You can push a modalViewController. To do that just make a new viewController with the scrollview and associated data in it, then
MyViewController *myViewController = [[MyViewController alloc] init];
[self presentModalViewController:myViewController animated:YES];
Create an IBAction in your new viewController and a hooked up button to that action to dismiss the modalView (something like this:
IBAction done {
[self dismissModalViewControllerAnimated:YES];
}
A couple options:
1) Create a new UIView object, either programmatically, or even in your existing XIB file. Use the [self.view addSubview:view] method to display it.
2) Create a new UIViewController with its own XIB file. Use [self presentModalViewController:anaimated:] to display it.
Either way, you'll need to add something to the new view to dismiss it when you're done.

Can't understand iPhone view layout

I have an iphone app that I built based off a tutorial (for a different framework so I had to modify things a bit) that uses a TabBar and a NavigationBar on the same View that also contains a UITable populated from an SQLite db.
I built it last night and when you select an item in the UITable it was redirecting to a view that displayed the detail of my item (at that point, just a city name). I went in and tried to modify that DetailView...and nothing I change works.
So I have a few questions/problems:
1) Why is it that my CityDetailView.xib looks like this in Interface Builder:
alt text http://www.jamespwright.com/images/public/detailviewinterfacebuilder.jpg
But looks like this when the app is run:
alt text http://www.jamespwright.com/images/public/wrongdetailview.jpg
I am new to iPhone development, so I'm not even sure where to begin looking for this problem.
I know that the TableViewController is set to CitiesTableViewController and within that controller code in my didSelectRowAtIndexPath I run this code:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
City *city = (City *)[appDelegate.cities objectAtIndex:indexPath.row];
// Initialize a new CityDetailView if there isn't one already
if (self.cityView == nil) {
CityDetailViewController *viewController = [[CityDetailViewController alloc] initWithNibName:#"CityDetailViewController" bundle:[NSBundle mainBundle]];
self.cityView = viewController;
[viewController release];
}
[appDelegate.citiesNavController pushViewController:cityView animated:YES];
self.cityView.city = city;
self.cityView.title = city.name;
[self.cityView.cityName setText:city.name];
[self.cityView.stateName setText:city.state];
[self.cityView.population setText:city.population];
And I have the labels linked up in Interface Builder to the properties in the Controller.
I also have the CityDetailView.xib class identity set to CityDetailViewController which has all the properties correctly declared.
One thing I can't figure out, if I rename CityDetailView.xib to "CityDetailView.xib1" the program still runs the same, but if I change [self.cityView.title = city.name]; to [self.cityView.title = #"Bob's Your Uncle!"]; it displays that change within the program.
On a completely unrelated note, on my TableView (my 2nd tab) I have this odd bar at the bottom that I don't know what it is or how to get rid of it, I'm pretty sure it has to do with the NavigationController but I don't know why or how it's there. It is this one:
alt text http://www.jamespwright.com/images/public/tabandnavigationview.jpg
Can anyone offer me any advice on these problems?
When "nothing happens" when you change source files, there are usually only a few things you're doing:
Dates are screwed up somewhere and you need to clean (Build → Clean)
You're editing the wrong file
You're building the wrong target
No, seriously, you're editing the wrong file, or building the wrong target
Cleaning is definitely the first thing to try. Especially if you can rename the XIB and everything still cheerfully goes on.
The simplest explanation is that self.cityView.stateName and the subsequent labels are not hooked up in interface builder. The controller must have a hooked up outlet to each label in order to populate it. Objective-C won't raise an error if you send a message to an object it can't find.