Xcode - 分段控制更改为默认值

Posted

技术标签:

【中文标题】Xcode - 分段控制更改为默认值【英文标题】:Xcode - Segmented Control changing to default 【发布时间】:2012-12-22 09:15:43 【问题描述】:

我有这个 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

【问题讨论】:

无法理解。发布您的段控制的一些代码。详细说明您的要求。 您想在按下段时返回第一个视图并立即返回第二个视图。但该段应保留其最后一次单击的位置。对吗? 不,我希望该段是某种设置,但是当我返回第一个视图时,它会变回默认值...我添加了一些代码 你在哪里推到第二个视图并弹出到第一个视图? 第一个视图有一个返回按钮 【参考方案1】:

如果我理解你的问题,这就是你的答案。

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

【讨论】:

不,我猜你不明白..我在同一个视图控制器上有 2 个视图,我在第二个视图上有段控制,每次我从第一个视图移动到第二个视图时,段控制选择第一个段(段 0)。

以上是关于Xcode - 分段控制更改为默认值的主要内容,如果未能解决你的问题,请参考以下文章

将 TEXT 列默认值从 null 更改为 ''(空字符串)

将默认消息“验证错误:需要值”更改为“需要值”

将 tinyint 默认值更改为 1 mysql

Xcode 4 UIPage 控件:如何更改页面控件的高度?

列表视图中的编辑文本值在屏幕超时时更改为默认值

Eclipse 设置已损坏。如何更改为默认值?