【问题标题】:Saving UILabel information?保存 UILabel 信息?
【发布时间】:2013-04-20 13:02:05
【问题描述】:

我正在为我的应用程序进行下一次更新,并且我正在尝试在应用程序关闭或切换页面时将信息保存在我的 UILabels 上。这就是我所拥有的,每次您按下按钮时,它都会更新我的 UILabel 以将前一个数字加 1,即值 = 0 但是当按下按钮时 = 1,但是每次我重新加载页面时它似乎都会重置,我想知道他们是否是一种保存该信息的方法,如果是的话,你怎么能这样做?下面是我的一些代码。

头文件

//Here I have created two labels that get updated when button pressed.
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;

}
//Here I have created two variables that correspond to the change in number.
@property (nonatomic) int i;
@property (nonatomic) int s;

实施文件:

//Here I have instantiated the two variables in my viewWillAppear method.
- (void)viewWillAppear{

self.i = 0;
self.s = 0;

}

//Here I have my button that changes the value of the label.
-(IBAction)randomButton {

self.i++;
[self->label1 setText:[NSString stringWithFormat:@"%d", self.i]];

提前致谢。回顾:如何保存我的 UILabel 的值,但允许它们在按下按钮时通过将值加 1 来更新?但我希望能够保存该价值并重新使用它。任何帮助将不胜感激。

更新: 第 1 页(这是我的主页)

第 2 页(我的统计页面/游戏页面)

我希望能够保存那个号码,它是一个 UILabel。但是,当我玩了一会儿并获得高数字,然后在第二页的左上角按回,然后在第一页按 start agian 时,数字恢复为 0。我怎样才能让它们保持它们的价值?

这是我的问题(在 .h 中):

这是我的问题(以 .m 为单位):

再次更新:我的 .h 现在没有问题,但我的 .m 有很多:

这是我的 AppDelegate.h

【问题讨论】:

    标签: ios objective-c save


    【解决方案1】:

    使用 NSUserDefaults 有什么问题吗?这样,它将在视图和应用程序关闭时保存。

    将以下内容添加到您的按钮操作中:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:aValue forKey:@"savedString1];
    

    第一行用于快速访问。然后将值(aValue)存储在“savedString1”字符串下。

    然后,根据您希望何时再次加载它,您会这样做:

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *returnAValue = [userDefaults valueForKey:@"savedString1"];
    

    这将声明一个具有先前值的新 NSString。您可以将其包含在 viewDidLoad 中。

    您也可以存储整数/其他内容 - 只需在加载时使用 integerForKey 而不是 valueForKey,并声明一个整数。

    希望有帮助吗?

    【讨论】:

    • 当我在应用程序中实现这一点时,我遇到了 aValue 的“未声明标识符”问题?​​
    • 用你的字符串/整数/其他名称替换'aValue'。
    • 另外请记住,如果它是一个整数,您需要预先声明您的整数(int randomText = 不管),然后将 aValue 替换为您要保存的变量的名称(randomText,对?)。加载时,您将需要使用 integerForKey 而不是 value。希望这对你有用!
    • 所以我在按钮中实现了顶部代码,但是我遇到了返回代码的问题,以及如何实现这一点。因为它的说法 returnAValue 也是一个未声明的标识符?
    • 把它放在你的按钮操作下: NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:randomText forKey:@"savedString1]; 这在你的 viewDidLoad 或任何你想加载它的地方: NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; int randomText = [userDefaults valueForKey:@"savedString1"]; 然后使用你的 randomText 什么的。如果你有任何问题,请告诉我。记住你需要用你需要的任何东西替换它,然后使用这些整数。
    【解决方案2】:

    编辑您的 AppDelegate.h

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    //add thist 2 properties
    @property (nonatomic, assign) NSUInteger counti;
    @property (nonatomic, assign) NSUInteger counts;
    
    @end
    

    然后为您的视图控制器编辑代码

    - (void)viewWillAppear:(BOOL)animated{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
        [self->label1 setText:[NSString stringWithFormat:@"%d", appDelegate.counti]];
        [self->label2 setText:[NSString stringWithFormat:@"%d", appDelegate.counts]];
    }
    
    -(IBAction)randomButton {
    
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
        appDelegate.counti = appDelegate.counti+1 ;
        [self->label1 setText:[NSString stringWithFormat:@"%d", appDelegate.counti]];
    
    
    }
    

    它应该可以工作

    【讨论】:

    • 你不必放最上面的代码,我只是举个例子:)
    • 说选择器“numberWithIntValue”没有已知的类方法? @adali
    • numberWithInt 或 numberWithInteger,抱歉打错了
    • 当我在应用程序上更改页面时它没有保存,然后返回?我已经插入了你告诉我的内容,究竟是什么? @adali
    • 您可以为您描述的操作添加更多图像吗?我不完全理解“更改页面”和“返回”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多