0

我有这个 xcode 应用程序,主要有 2 个视图控制器,有 2 个按钮(第一个是弹出警报视图的按钮,第二个是在视图之间移动的按钮)。在第二个视图中,我有一个带有 4 个段的分段控制器,当您选择其中一个时,返回到第一个视图,然后返回到分段控制器视图,分段控制器返回到第一个段......我该如何修复这个 ?

这是 .m 文件中的代码

//
//  ViewController.m
//  iBored
//
//  Created by Yannai on 12/15/12.
//  Copyright (c) 2012 Yannai Harel. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    boredInt = arc4random()%30+1;
    boredString = [[NSString alloc]init];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)weatherButton:(id)sender {

}

-(void)iBoredAction {
    switch (boredInt) {
        case 1:
            boredString = @"Draw!";
            break;
        case 2:
            boredString = @"Try to earn money!";
            break;
        case 3:
            boredString = @"Climb on trees!";
            break;
        case 4:
            boredString = @"Do homework!";
            break;
        case 5:
            boredString = @"Watch TV!";
            break;
        case 6:
            boredString = @"Go on a run!";
            break;
        case 7:
            boredString = @"Connect to your innerself - meditate!";
            break;
        case 8:
            boredString = @"Play on the computer!";
            break;
        case 9:
            boredString = @"Call a friend!";
            break;
        case 10:
            boredString = @"Go to a friend!";
            break;
        case 11:
            boredString = @"Play with your pet!";
            break;
        case 12:
            boredString = @"Take your pet on a walk!";
            break;
        case 13:
            boredString = @"Play on an instrument - if you don't know try to learn!";
            break;
        case 14:
            boredString = @"Watch youtube videos!";
            break;
        case 15:
            boredString = @"Listen to music!";
            break;
        case 16:
            boredString = @"Read a book!";
            break;
        case 17:
            boredString = @"Go to iFunny!";
            break;
        case 18:
            boredString = @"Watch a movie!";
            break;
        case 19:
            boredString = @"Do some experiments!";
            break;
        case 20:
            boredString = @"Play soccer!";
            break;
        case 21:
            boredString = @"Play basketball!";
            break;
        case 22:
            boredString = @"Go for a ride on your bike!";
            break;
        case 23:
            boredString = @"Do some exercises!";
            break;
        case 24:
            boredString = @"Build a treehouse!";
            break;
        case 25:
            boredString = @"Build a fort!";
            break;
        case 26:
            boredString = @"Record short and cool films!";
            break;
        case 27:
            boredString = @"Start programming!";
            break;
        case 28:
            boredString = @"Go online!";
            break;
        case 29:
            boredString = @"Do some research about a subject you like!";
            break;
        case 30:
            boredString = @"Go outside and stare at the sky!";
            break;

        default:
            break;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"iBored" message:boredString delegate:self cancelButtonTitle:@"Nice Idea" otherButtonTitles:@"Pass",nil];
    [alert show];
}



-(IBAction)switchWeatherSwitch:(id)sender {
    if (weatherSwitch.on) {
        weatherOut.enabled = YES;
        NSLog(@"on");
    }
    else {
        weatherOut.enabled = NO;
        NSLog(@"off");
    }
}


- (IBAction)iBored:(id)sender {
    if (weatherOut.selectedSegmentIndex == 0) {
        NSLog(@"Sunny");
    }
    else if (weatherOut.selectedSegmentIndex == 1) {
        NSLog(@"Rainy");
    }
    else if (weatherOut.selectedSegmentIndex == 2) {
        NSLog(@"Windy");
        [self iBoredAction];
    }
    else if (weatherOut.selectedSegmentIndex == 3) {
        NSLog(@"Snowy");
    }

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self iBoredAction];
    }
}
@end
4

1 回答 1

0

If I understand your question, this will be your answer.

@interface firstViewController : UIViewController{

    firstViewController *secondView;
}

//add this where you push to the secondView

if(!secondView)
    secondView = [[secondViewController alloc] init];
//This will init the second view only one time. So the position of your segments will retain its state.
    [[self navigationController]pushViewController:photocontent animated:YES]
于 2012-12-22T09:50:30.503 回答