开发者

iOS utility application-flipside view settings

开发者 https://www.devze.com 2023-03-28 19:03 出处:网络
I am making a utili开发者_如何学编程ty application, and I have a label on my main view. Depending on the user\'s selection, that label should say one of two things. How can I use a segmented control t

I am making a utili开发者_如何学编程ty application, and I have a label on my main view. Depending on the user's selection, that label should say one of two things. How can I use a segmented control to change the label text?


In your flipside, create an IBAction called segmentedControlChanged - and hook it up to the 'value changed' trigger on the segmented control.

At the top of your settings controller, declare this constant:

#define kSegmentKey @"SegmentSetting" // Call these whatever you want

In your segmentedControlChanged method, write to NSUserDefaults, like so:

- (IBAction)segmentedControlChanged:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:[sender selectedSegmentIndex] forKey:kSegmentKey];
}

In your main view controller's viewWillAppear, put the following code:

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int setting = [defaults integerForKey:kSegmentKey];
    if (setting == 0) {
        myLabel.text = @"First Message";
    }
    else {
        myLabel.text = @"Second Message";
    }
}


You can:

(1) store the segmented control's value in a variable that's passed as a return value to the FlipsideViewControllerDidFinish method, or else

(2) store the value in some area accessible to both the Flipside view and the First view, such as [NSUserDefaults standardUserDefaults].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号