Need Help switching views...

Discussion in 'iOS Programming' started by EducationApp, Aug 17, 2011.

  1. EducationApp macrumors newbie

    Joined:
    Aug 17, 2011
    #1
    I'm stuck on how to make it so the user has to click one button, and then a second for switching views. I'm trying to make it so the user can change the difficulty of the game, Easy, Medium, or Hard, and then press start to begin. How to I go about doing this. Is it an "if" statement. I've named the buttons btnButton1, btnButton2, btnButton3, btnButton4. Can you supply the code for me to switch views with just the easy option and then start?
    The other views are imported as: easyview, mediumview, hardview.
    Thanks
     
  2. EducationApp, Aug 17, 2011
    Last edited: Aug 17, 2011

    EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #3
    I know how to change views by clicking one button and then having it change but I'm stuck on how to have it so the Easy button + Ok goes to the Easy page and the medium button + ok goes to the medium button.
    I have no idea where to go...
     
  3. EducationApp, Aug 17, 2011
    Last edited: Aug 17, 2011

    EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #4
    -(IBAction)switchview:(id)sender {
    easyview *easy = [[easyview alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:easy animated:YES];
    }


    -(IBAction)btnButton1:(id)sender
    {
    infoText.text = @"EASY: You have 10 rounds to complete and you are"
    " given 60 seconds per round."
    " Deal when ready...";
    if (infoText.text == @"EASY: You have 10 rounds to complete and you are"
    " given 60 seconds per round."
    " Deal when ready...") {
    -(IBAction)btnButton4 = switchview;
    }
    }
    -(IBAction)btnButton4 = switchview doesn't work...
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #5
    Please surround your code with
    Code:
     tags so that we can read it, and post the entire code for the class you are having trouble with.
     
  5. phamtuanchip macrumors newbie

    Joined:
    Aug 17, 2011
  6. EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #7
    //
    Code:
    
    #import "thirdview.h"
    #import "secondview.h"
    #import "easyview.h"
    #import "mediumview.h"
    #import "hardview.h"
    
    @implementation thirdview
    @synthesize infoText;
    
    
    -(IBAction)switchview:(id)sender {
        easyview *easy = [[easyview alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:easy animated:YES];
    }
    
    
    -(IBAction)btnButton1:(id)sender
    {
        infoText.text = @"EASY: You have 10 rounds to complete and you are" 
            " given 60 seconds per round."      
            "                                                                   Deal when ready...";
        if (infoText.text == @"EASY: You have 10 rounds to complete and you are" 
            " given 60 seconds per round."      
            "                                                                   Deal when ready...") {
    
        }
       }
    
    
    
    -(IBAction)btnButton2:(id)sender
    {
        infoText.text = @"MEDIUM: You have 15 rounds to complete and you are given 40 seconds per round."
        "                                                           Deal when ready...";
    }
    
    
    -(IBAction)switchview1:(id)sender {
        mediumview *medium = [[mediumview alloc] initiWithNibName:nil bundle:nil];
        [self presentModalViewController:medium animated:YES];
    }
    
    
    
    
    -(IBAction)btnButton3:(id)sender
    {
        infoText.text = @"HARD: You have 20 rounds to complete and you are given 20 seconds per round."                                       
        "                                                           Deal when ready...";
    }
    
    
    
    -(IBAction)switchback:(id)sender {
        secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:second animated:YES];
    }
    
    
    
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [label setFont: [UIFont fontWithName:@"Speedline" size:24]];
        [super viewDidLoad];
            infoText.text = @"Welcome to Math Jack!";
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)viewDidUnload
    {
    
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    -(void)dealloc {
        [super dealloc];
        
        [infoText release];
        
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    
    
    @end
    
    //



    Didn't complete the if statement because when I put "-(IBAction)btnButton4 = switchview;" in, there is an error with the IBAction.
     
  7. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #8
    Which of your IBAction methods are actually hooked up to the button presses?
     
  8. EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #9
    Code:
    
    #import <UIKit/UIKit.h>
    
    @interface thirdview : UIViewController {
        
        UILabel *infoText;
        IBOutlet UILabel *label;
    
    
    }
    -(IBAction)switchback:(id)sender;
    -(IBAction)switchview:(id)sender;
    -(IBAction)switchview1:(id)sender;
    
    @property (nonatomic, retain) IBOutlet UILabel *infoText;
    
    -(IBAction)btnButton1:(id)sender;
    -(IBAction)btnButton2:(id)sender;
    -(IBAction)btnButton3:(id)sender;
    -(IBAction)btnButton4:(id)sender;
    
    @end
    
    
     
  9. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #10
    That code doesn't answer my question. I want to know which of those IBAction methods you have wired to the button presses in Interface Builder.

    I'm going to assume that it's the btnButton1-4 ones that are wired to your four buttons. It looks like you want the code in the switchview method to execute after the code in btnButton1- so why not just put the code that is currently in the switchview method in btnButton1, or call the switchview method at the end of btnButton1?

    P.S. You need to learn proper Objective-C naming conventions.
     
  10. EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #11
    It is because the text shows up and they need to first press Easy, read the infoText, if they want to play that difficulty, "Deal" takes them there, if they want it harder, they go to Medium or Hard
     
  11. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #12
    OK, I think I understand now. What IBAction is the Deal button wired to?
     
  12. EducationApp thread starter macrumors newbie

    Joined:
    Aug 17, 2011
    #13
    I'd like to start off with thanking you for helping me. The Deal button is btnButton4 which can be seen on the thirdview.m but I have not put it in the thirdview.h. All the switchviews go through it though.
     
  13. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #14
    Well the concise answer to your question is that you should probably have an instance variable that stores the user's current difficulty selection, and when Deal is pressed, you will look at that variable and decide what view to load with an if or switch statement. This means you will only need 4 IBAction methods.
     

Share This Page