2012-12-22 53 views
0

我有這個xcode應用程序,有2個視圖控制器在主要有2個按鈕(第一個是一個按鈕,彈出一個alertview和第二個是一個按鈕之間移動視圖)。在第二個視圖中,我有一個分段的4段控制器,當您選擇其中一個時,返回到第一個視圖,然後返回到分段控制器視圖,分段控制器返回到第一個段......我該如何解決這個 ?Xcode - 分段控件更改爲默認

這裏是.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 
+0

看不懂。發佈您的細分控制的一些代碼。更多地解釋你的需求。 –

+0

您想要在按下分段時立即回到第一個視圖並立即轉到第二個視圖。但該細分受衆羣應保留最後一次點擊的位置。對? –

+0

不,我想段是設置的排序,但當我回到第一個視圖,然後它變回默認...我添加了一些代碼 – user1289671

回答

0

代碼如果我明白你的問題,這將是你的答案。

@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] 
+0

nahh我猜你不明白..我有2個視圖在同一個視圖控制器上,我有第二個視圖的段控制,每次我從第一個視圖移動到第二個視圖段控件選擇第一個段(段0)。 – user1289671