2012-12-22 54 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