PDA

View Full Version : iPhone SDK UIView/UIViewController Question




Sionice
Jan 12, 2009, 12:28 PM
Hi there,

As usual, I'm new to the iPhone SDK and Objective C, though I currently have a basic application with a MainView.h that is tied into my Window/view via IB. It's declared using the UIView subclass

@interface MainView : UIView {

It's opened in my AppDelegate class with the following lines.
[mainView loadView];
[window makeKeyAndVisible];

I have several buttons on the view, and would like to use one of those buttons to transition to an alternate view. I can create the .xib file, though get extremely confused when I try to figure out how to link the button press to the pushing of a new view onto the stack.

What are the steps that I need to take to add a secondary view to my project, open that view with a standard button press, and then return to the main view? I don't really care if help is provided via IB or strictly in code. Any help you're able to provide is much appreciated.

Thanks,
- Sionice



Spike099
Jan 12, 2009, 12:47 PM
It sounds like you want to use UINavigationController (http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/UsingNavigationControllers/chapter_7_section_1.html#//apple_ref/doc/uid/TP40007457-CH7-SW1).

Explaining each step needed to do that would be a little lengthy, try using that guide first. Let us know if you have any trouble getting through it.

Sionice
Jan 12, 2009, 01:00 PM
It sounds like you want to use UINavigationController (http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/UsingNavigationControllers/chapter_7_section_1.html#//apple_ref/doc/uid/TP40007457-CH7-SW1).

Explaining each step needed to do that would be a little lengthy, try using that guide first. Let us know if you have any trouble getting through it.

Thanks for the reply; much appreciated. Though wouldn't that require the use of a navigation bar on the first/main page then? Or am I mistaken? I don't mind using a navigation bar on the second page to set a 'back' button in the upper left, though I want to make sure that using that feature doesn't require the use of such a UI element on the primary page.

However, I'd prefer to navigate between views simply on a custom button press event, rather than with a pre-built UI.

Spike099
Jan 12, 2009, 01:16 PM
Yes you would have a navigation bar at the top of your first view. I figured you wouldn't mind having that.

What you want to do. In IB, setup both your views how you want them with buttons and all.

Im typing this code manually, so it may not be very accurate, but hopefully it helps.

In your AppDelegate.h

@interface AppDelegate:UIView {
IBOutlet UIView *mainView;
IBOutlet UIView *secondView;
}

- (IBAction)gotoMainView:(id)sender;
- (IBAction)gotoSecondView:(id)sender;

@end

In your AppDelegate.m

@implementation

- (IBAction)gotoMainView:(id)sender{
[window setContentView:mainView];
}
- (IBAction)gotoSecondView:(id)sender {
[window setContentView:secondView];
}

@end

In IB, CTRL-Drag from your button to your File's Owner and select gotoSecondView. And do the same thing for the other button to return to the mainview...

Also CTRL-Drag from the File Owner to each view assigning them respectively.

dejo
Jan 12, 2009, 01:17 PM
What you desire is possible. In the button-press code, I would suggest calling presentModalViewController for your new UIViewController that will handle your second view.

Sionice
Jan 12, 2009, 01:59 PM
Yes you would have a navigation bar at the top of your first view. I figured you wouldn't mind having that.

What you want to do. In IB, setup both your views how you want them with buttons and all.

Im typing this code manually, so it may not be very accurate, but hopefully it helps.

In your AppDelegate.h

@interface AppDelegate:UIView {
IBOutlet UIView *mainView;
IBOutlet UIView *secondView;
}

- (IBAction)gotoMainView:(id)sender;
- (IBAction)gotoSecondView:(id)sender;

@end

In your AppDelegate.m

@implementation

- (IBAction)gotoMainView:(id)sender{
[window setContentView:mainView];
}
- (IBAction)gotoSecondView:(id)sender {
[window setContentView:secondView];
}

@end

In IB, CTRL-Drag from your button to your File's Owner and select gotoSecondView. And do the same thing for the other button to return to the mainview...

Also CTRL-Drag from the File Owner to each view assigning them respectively.

That sounds like what I want. Thanks a lot for taking the time to respond; I'll give it a try here in a few minutes when I can get away from work. I'll post the results.

Sionice
Jan 12, 2009, 06:14 PM
Thanks a lot for your help on this one; what you suggested worked perfectly for what I need. I went with the addSubview method.

I had already tried something similar and that didn't work out. Turned out though that my problem was my knowledge and use of xCode. For some reason it wasn't rebuilding my file after I had made changes to it, so all I had to do was add those changes, do a clean all, and rebuild.

I'm not quite sure why that happened, though I know I've had similar issues with Visual Studio in the past as well.

Once again, thanks for taking the time to help out.

joshsroka
Jan 12, 2009, 10:43 PM
I added this to my program but it isnt working, did I add my 2nd xib correctly by clicking add file then user interface and view xib ? If so I wonder why this didnt work for me thanks

firewood
Jan 13, 2009, 06:45 PM
Put aside your project, and go though the all video tutorials on using IB, maybe several times.

.

Spike099
Jan 14, 2009, 10:25 AM
I added this to my program but it isnt working, did I add my 2nd xib correctly by clicking add file then user interface and view xib ? If so I wonder why this didnt work for me thanks

I was assuming that the second view was within the same xib. While your beginning, just use the same xib.