2

所以我有两节课。当按下保存按钮时,它会将值从self.screen.textbyaddItem方法传递到totalArray类 2。如果我尝试NSLog@implementationofaddItem方法中,那么它将给出正确的输出,但如果我在 中执行viewDidLoad,则输出为空。如何永久保存从 class1 传递到 class2 属性的值?谢谢你。UITableViewController 子类中的 class2

1 类@interface

//class1.h
#import class2.h
@interface class1 : superclass {

}

- (IBAction)buttonSave:(id)sender;

Class1 @implementation

//class1.m
@interface class1 ()

@end

@implementation class1 {

}

- (IBAction)buttonSave:(id)sender {
  class2 *Obj = [[class2 alloc] init];
  [Obj addItem:self.screen.text];
}

和 class2 @interface

//class2.h
#import class2.h
@interface {

}

@property (strong, nonatomic) NSMutableArray *totalArray;

类 2 @实现

@interface class2 ()

@end

@implementation {

}

- (void) addItem:(id)item {
  self.totalArray = [[NSMutableArray alloc] init]; //alloc & init
  [self.totalArray addObject:item]; //add object to the total array
  // NSLog(@"%@", self.totalArray); If I NSLog in within this method then everything works as expected.
}

- (void)viewDidLoad {
  [super viewDidLoad];

  NSLog(@"%@", self.totalArray); //But in here the output is null. ???

}
4

4 回答 4

0

You can not ensure when your viewDidLoad method will call... so better pass the value to the init method and set there initWithText:(NSString*)text{}. Other wise try to call NSLog in viewWillAppear or viewDidAppear just for testing purpose. In iOS 7 now presentation of view-controllers is bit changed now.

于 2013-10-22T08:23:05.517 回答
0

尝试像这样使用...

- (IBAction)buttonSave:(id)sender 
    {
      class2 *Obj = [[class2 alloc] init];

      Obj.totalArray = [[NSMutableArray alloc] init]; //alloc & init
      [Obj.totalArray addObject:self.screen.text];
      NSLog(@"screen.text %@", self.screen.text); // -- check here it may be null---- 
      NSLog(@"Obj.totalArray %@", Obj.totalArray);

    }

    @interface class2 ()

    @end

    @implementation {

    }

    - (void)viewDidLoad
     {
      [super viewDidLoad];

      NSLog(@"%@", self.totalArray); //But in here the output is null. ???

    }
于 2013-10-22T07:30:29.537 回答
0

viewDidLoad之后调用,init所以你的数组在nil这里。更改您的class2 init方法以接受该项目。

// In class2
-(id) initWithStyle:(UITableViewStyle)style andItem:(id)item {
    self = [super initWithStyle:style];
    if(self) {
        self.totalArray = [[NSMutableArray alloc] init];
        [self.totalArray addObject:item];
    }
    return self;
}

然后您的 addItem 将如下所示,

- (void) addItem:(id)item {
  //Just add, do not initialize again
  [self.totalArray addObject:item];
}

class1 中的按钮操作现在看起来像,

- (IBAction)buttonSave:(id)sender {
    class2 *Obj = [[class2 alloc] initWithItem:self.screen.text];
    //OR
    //class2 *Obj = [[class2 alloc] initWithItem:UITableViewStylePlain andItem:self.screen.text];
}

希望有帮助!

于 2013-10-22T07:32:56.047 回答
0

我认为您的问题是您使用了不同的 class2 对象。您在 buttonSave 中初始化的那个不是您正在显示的那个

在 class1.h 中添加一个属性

@property (nonatomic, strong) NSMutableArray *savedArray;

并修改 buttonSave :

- (IBAction)buttonSave:(id)sender {
      self.savedArray = [[NSMutableArray alloc] init];
      [self.savedArray addObject:self.screen.text];
} 

class2Segue您正在使用情节提要,然后请尝试将其添加到 class1.h 并在情节提要中向此 segue添加标识符:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"class2Segue"]) 
    {
        Class2 *tableController = (Class2 *)[segue destinationViewController];
        tableController.totalArray = self.savedArray;
    }
}
于 2013-10-22T08:18:56.800 回答