Ios self始终在viewController';s方法
由于某种原因,我遇到了一个问题,在viewController的代码中(在调用viewDidLoad方法后运行良好),self总是返回nil。(在iPadAir上运行,iOS 9.2.1,以9.2为目标构建的应用程序) 在初始化代码中,我们有一个普通的初始化程序:Ios self始终在viewController';s方法,ios,uiviewcontroller,ios9,Ios,Uiviewcontroller,Ios9,由于某种原因,我遇到了一个问题,在viewController的代码中(在调用viewDidLoad方法后运行良好),self总是返回nil。(在iPadAir上运行,iOS 9.2.1,以9.2为目标构建的应用程序) 在初始化代码中,我们有一个普通的初始化程序: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { }; self = [super initWith
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
};
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}
viewController的实例化如下所示:
myVC * myVC = [[myVC alloc] initWithNibName:nil bundle:nil];
- (void) networkPropertiesDidChange:(NSDictionary *)properties {
void (^completionBlock)(UIAlertAction *) = ^(UIAlertAction *action) {
[self.serialNumberField setEnabled:false];
};
if([properties[@"result" isEqualToString @"error"]) {
[Utility showBasicAlertControllerWithText:@"Device not registered." completionBlock:completionBlock sender:self];
return;
}
else if ([properties[@"result"] isEqualToString @"ipChange"]) {
NSString *newAddy = [[NSString alloc] initWithString:properties[@"ipAddress"]];
self.ipAddressLabel.text = newAddy;
return;
}
else return;
}
myVC类和.xib文件的名称相同
有一个属性叫做:
@property (weak, nonatomic) IBOutlet UILabel *ipAddressLabel;
在myVC内部的一个方法中,我有这样一个方法:
myVC * myVC = [[myVC alloc] initWithNibName:nil bundle:nil];
- (void) networkPropertiesDidChange:(NSDictionary *)properties {
void (^completionBlock)(UIAlertAction *) = ^(UIAlertAction *action) {
[self.serialNumberField setEnabled:false];
};
if([properties[@"result" isEqualToString @"error"]) {
[Utility showBasicAlertControllerWithText:@"Device not registered." completionBlock:completionBlock sender:self];
return;
}
else if ([properties[@"result"] isEqualToString @"ipChange"]) {
NSString *newAddy = [[NSString alloc] initWithString:properties[@"ipAddress"]];
self.ipAddressLabel.text = newAddy;
return;
}
else return;
}
然而,地址标签只是变成空白
当我在代码执行时单步执行代码时,在初始化newAddy的行上,self不是nil,ipAddressLabel不是nil。但是,当运行时点击self.ipAddressLabel.text时,它将通过initWithNibName,并为self返回nil!然后ipAddressLabel被设置为nil,然后它在视图上变为空白
此时,myVC已成功加载并显示在屏幕上。所以我无法理解为什么赛尔夫在这个方法中为赛尔夫返回零。。。这很奇怪
如果我删除对initWithNibName方法的重写,那么一切都可以完美地工作。如果我在设置self=[super…]之前在initWithNib name的开头检查(self!=nil),则视图在屏幕上的绘制距离太低约1英寸,并被切断
只是想弄明白为什么会发生这种情况。谢谢。问题在这里:
void (^completionBlock)(UIAlertAction *) = ^(UIAlertAction *action) {
[self.serialNumberField setEnabled:false];
};
它必须是软弱的自己
__weak typeof(self) weakSelf = self;
void (^completionBlock)(UIAlertAction *) = ^(UIAlertAction *action) {
[weakSelf.paxSerialNumberField setEnabled:false];
};
不要问我为什么,我的大脑爆炸了。只是一些想法:1)用NSLog添加一个dealloc方法,看看它是否被调用(出于某种原因,这个对象没有被保留)-2)为你的ipAddressLabel添加你自己的客户设置器,当它设置为某个值时记录它(看看它是否为零)。添加断言(或NSAsserts)所有用于测试ipAddressLabel是否为nil的方法。无法添加dealloc,我正在使用ARC。我已尝试添加自定义setter,但无法对nil对象调用方法。我添加了断言,并且在调用
self.ipAddressLabel.text=newAddy;
…dealloc仍在ARC下发送之前,ipAddressLabel不是nil