0

在关闭 FlipsideViewController 之前,我有这些值

shours
sminutes
sseconds
srecharge
change

从宣言

int shours;
int sminutes;
int sseconds;
int srecharge;
bool change;

现在我想将这些变量传递给这些其他变量中的另一个 UIViewController (MainViewController)

mhours
mminutes
mseconds
mrecharge
mchange

最简单的方法是什么?

4

3 回答 3

0

我会创建一个类来保存这些值。然后在视图控制器之间传递这个对象。

像这样的东西:

接口文件:

//
//  MyObject.h
//  SOObjectPassing
//
//  Created by Wilson, LJ on 11/15/12.
//  Copyright (c) 2012 Arkansas Children's Hospital. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MyObject : NSObject {
    int shours;
    int sminutes;
    int sseconds;
    int srecharge;
    bool change;
}

@property (nonatomic, assign) int shours;
@property (nonatomic, assign) int sminutes;
@property (nonatomic, assign) int sseconds;
@property (nonatomic, assign) int srecharge;
@property (nonatomic, assign) BOOL change;

-(id) initWithHours:(int)hours
            minutes:(int)minutes
            seconds:(int)seconds
           recharge:(int)recharge
             changed:(BOOL)changed;

@end

实现文件:

//
//  MyObject.m
//  SOObjectPassing
//
//  Created by Wilson, LJ on 11/15/12.
//  Copyright (c) 2012 Arkansas Children's Hospital. All rights reserved.
//

#import "MyObject.h"


@implementation MyObject
@synthesize shours = _shours;
@synthesize sminutes = _sminutes;
@synthesize sseconds = _sseconds;
@synthesize srecharge = _srecharge;
@synthesize change = _change;

-(id) initWithHours:(int)hours
            minutes:(int)minutes
            seconds:(int)seconds
           recharge:(int)recharge
             changed:(BOOL)changed {

    if ((self = [super init])) {
        _shours = hours;
        _sminutes = minutes;
        _sseconds = seconds;
        _srecharge = recharge;
        _change = changed;
    }

    return self;
}
@end

并像这样实例化您的对象:

MyObject *myObject = [[MyObject alloc] initWithHours:2
                                                 minutes:3
                                                 seconds:4
                                                recharge:1
                                                 changed:NO];

然后只需将整个对象传递给您的其他 VC。

这是说明这一点的示例项目。

于 2012-11-15T16:08:04.957 回答
0

我只需创建一个“模型”类来保存这些变量并将模型类从一个视图控制器传递到下一个视图控制器。一旦它在从 view1 到 view2 的转换过程中链接到 view1 和 view2 中,它就会在从 view2 转换回 view1 时自动更新。使用 @property (assign) int shours 来创建 setter 和 getter。

使用这种方法,使用 segues 就可以了。你不需要使用 NSUserDefaults 或 NSNotificationCenter。

于 2012-11-15T15:59:41.510 回答
0

在您上创建一个自定义对象属性MainViewController并将其设置在您的FlipsideViewController或由于某种原因您不能这样做:创建一个对象来保存这些值并将其放入NSUserDefaults并继续阅读MainViewController

于 2012-11-15T14:24:20.687 回答