如何在plist中存储和检索UISegmentedControl

时间:2012-03-19 07:16:40

标签: iphone objective-c cocoa-touch ios-simulator uisegmentedcontrol

我想将分段控件存储到属性列表中,也可以从plist中检索。 那我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

在viewcontroller中创建一个UISegmentControl Outlet

IBOutlet UISegmentedControl *mySegment;

将其连接到Interface Builder中的UISegmentControl

添加此IBAction方法并将其连接到Interface Builder

- (IBAction) changeSegment: (id) sender {
    //save UISegment value in NSUserDefaults
    [[NSUserDefaults standardUserDefaults] setInteger:[sender selectedSegmentIndex] forKey:@"savedSegment"];
}

在viewDidLoad

中加载视图时恢复保存的值
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.mySegment.selectedSegmentIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"savedSegment"];
}