Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sionice

macrumors newbie
Original poster
Jan 12, 2009
4
0
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

Code:
@interface MainView : UIView {

It's opened in my AppDelegate class with the following lines.
Code:
[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
 

Sionice

macrumors newbie
Original poster
Jan 12, 2009
4
0
It sounds like you want to use UINavigationController.

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

macrumors regular
Feb 18, 2007
143
0
Canada
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

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

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

@end
In your AppDelegate.m

Code:
@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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors newbie
Original poster
Jan 12, 2009
4
0
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

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

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

@end
In your AppDelegate.m

Code:
@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

macrumors newbie
Original poster
Jan 12, 2009
4
0
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

macrumors member
Oct 2, 2008
39
0
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
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.