从另一个类获取值后,NSInteger保持为null

时间:2017-05-02 06:08:59

标签: ios objective-c swift row

我想要做的是从另一个类中使用方法获取NSInteger,然后使用我在IBAction中获得的值。

所以在我的.h文件中我有:

@property (nonatomic) NSInteger row;
-(void) selectedRow : (NSInteger) selectedRow;
-(IBAction)changeName:(id)sender;

在我的.m文件中:

@synthesize row;

-(void) selectedRow : (NSInteger) selectedRow {

    row = selectedRow; //I tried also self.row

}

-(IBAction)changeName:(id)sender {

    NSLog(@"row = %lu", (long)row); //To keep it simple.

当我在行调试时,它告诉我,该行获得了另一个类的值,但在IBAction中它是0。

请有人告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个......

-(void) selectedRow : (NSInteger) selectedRow {

    //row = selectedRow; //I tried also self.row
    [[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:@"Row"];
}

-(IBAction)changeName:(id)sender {

    NSInteger selectedRow = [[NSUserDefaults standardUserDefaults] integerForKey:@"Row"];
    NSLog(@"row = %ld",(long)selectedRow); //To keep it simple.
}

Swift 4

func selectedRow(selectedRow:Int) -> Void {

    UserDefaults.standard .set(selectedRow, forKey: "Row")
}

@IBAction func changeName(_ sender: AnyObject) {

    let selectedRow = UserDefaults.standard .integer(forKey: "Row")
    print("row = \(selectedRow)")
}