我有一个超级简单的应用程序,具有根视图控制器和模态视图控制器。
在第一个视图中,我只有一个UIButton - 它动画过渡到模态控制器。在模态视图控制器中我有4个滑块(R,G,B,Alpha),它们正在改变模态视图背景颜色和一个UIBUtton以退出模态视图。通过单击此按钮,我的根控制器的背景颜色需要更改为与模态控制器中相同的颜色。我怎么能这样做并让我的模态控制器采取行动来实现这个目标呢?
另外,我需要将此首选项保存到NSUserDefaults。那么最正确的方法是什么?
答案 0 :(得分:2)
您好这里有一些关于NSUserDefaults可以做的事情的快速参考。您可以使用它来保存NSUserDefault中的数据
保存
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
检索
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];
你可以使用像CGFloat red = [myColor red]这样的工具来获取RGB值;和CGFloat Green = [myColor Green];喜欢明智并用它来拯救他们。
或者您可以查看Get RGB value from UIColor presets相同的
答案 1 :(得分:1)
阅读委托模式,做第一件事;我发布了一个我在下面写的小教程。另一个答案看起来应该足够NSUserDefaults
Delegates
//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"segueName"])
{
childController *foo = segue.destinationViewController;
foo.delegate = self;
}
}
//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
//An example of what you could do if your data was an NSDate
buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}
//In parent .h file:
//import child header
#import "ChildName.h"
//indicate conformity with protocol
@interface ParentName : UIViewController <ChildNameDelegate>
//In child .h file
//declare protocol
@protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
@end
//declare delegate
@property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;
//In child .m file
//synthesize delegate
@synthesize delegate;
//use method
- (IBAction)actionName:(id)sender
{
[delegate methodName:assignedData];
}
这是另一个例子Simple Delegate Example?