How do you switch between views?

Discussion in 'iOS Development' started by bamhm182, May 25, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I'm trying to figure out how to make it so that when I push a button on my menu, it takes me to another screen. How can I do that? I tried a method that was in this book I have, and I searched, but neither of those worked, so I figured I'd ask.

    Also, in the view that I'm going to, I tried to put a few UIImageViews, however, when I click on Simulate Interface, the pictures don't show up? I wanted to have a header image and a background image in the UIScrollView. Is adding UIImageViews the correct way to go, or is there a better way?

    One last thing, I put in an action sheet, so when I hit the top button on my menu, it pops up asking where it wants to go, but I get an error. This is what I have:

    Code:
    - (IBAction)playButtonPressed:(id)sender;
    {
    	UIActionSheet *actionSheet = [[UIActionSheet alloc]
    					initWithTitle:@"Choose one"
    					delegate:self
    					cancelButtonTitle:nil
    					destructiveButtonTitle:nil
    					otherButtonTitles:@"Game Type 1", @"Game Type 2", @"Game Type 3", @"Cancel", nil];
    					<warning: class '*ViewController' does not implement the 'UIActionSheetDelegate' protocol>
    	actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    	actionSheet.destructiveButtonIndex = 3;
    	[actionSheet showInView:self.view];
    	[actionSheet release];
    }
    How can I fix that?
  2. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    in that header file

    You see something like @interface class : UIViewController

    you should add something like this UIViewController <UIActionSheetDelegate>
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I just tried that, and it gave me about 60 errors...

    You meant like this, right?:

    Code:
    @interface *ViewController : UIViewController {
    	IBOutlet	UIButton	**Button;
    	IBOutlet	UIButton	**Button;
    	IBOutlet	UIButton	**Button;
    	IBOutlet	UIView		**ViewController;
    	IBOutlet	UIView		**ViewController;
    	UIViewController		<UIActionSheetDelegate>
    }
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Code:
    @interface *ViewController : UIViewController <UIActionSheetDelegate>
  5. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Oh, right, sorry, I wasn't thinking, makes SO much sense now...

    Any help with switching to another view?
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Don't put the asterisk in there...
  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Yeah don't ask why I put an asterisk on there, maybe I did it by accident. Maybe I'm just an idiot. As for adding view

    Code:
    [self.view addSubview:someOtherView];
    If you want animated view changing look at some of skylars examples on CATransitions or UIViewAnimations.
  8. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    Well yeah, of course I didn't put the asterisk in there. I was just using that as a wild card. I don't want to reveal what I'm working on until I release it.

    Please Register or Log in to view images



    EDIT: Oh, I thought you were talking about how I had many asterisks in my code a few posts back. I knew not to put chicken4eve's asterisk in it.

    Thanks chiecken4eve! I've had some experience with the transition effects, but I'll look into skylar's examples anyways.

    EDIT 2: I think we've got a little miscommunication on what I'm trying to do. I don't want a subview, I think... I've got 2 xibs, 1 xib is the menu, and the other is the scores. I have a button on the menu xib that when pressed, should switch to the scores xib. How can I do that?

    I've got the button set up as an IBOutlet and it's all implemented, now all I need it the code to put in the sender.

    To make sure I've got this right, a SubView would be a view that's in one xib, right? For example, you have a menu XIB, than inside of it, you have multiple views, if one of those views was referenced as scoresView, than putting:

    Code:
    - (IBAction)scoresButtonPressed:(id)sender;
    {
    	[self.view addSubview:scoresView];
    }
    in the .m and adding the correct IBOutlets, than referencing the scoresView as the scoresView would be the correct way to make the score button go to the scores view, right?

    Also, I was thinking that having multiple XIBs would be the correct way to go because my game is going to have multiple game types, each with multiple levels. So I was thinking I'd have an XIB for Game Type 1, Game Type 2, etc, than use SubViews for each level. Would this be a good way to go about it?
  9. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    I would suggest learning the use of UINavigationControllers instead of just drawing random views on top of each other.
  10. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Seeing as you are using XIBS and NIBS as far as I can tell you want something like

    Code:
    - (void)someAction {
            MainViewController *mainView = [[MainViewController alloc] initWithNibNamed:@"SomeNib" bundle:[NSBundle mainBundle]];
            [self.view addSubview:mainView.view];
    }

Share This Page