[self.view viewWithTag]返回nil问题

uxid 发布于 2015年04月11日 | 更新于 2015年04月13日
无人欣赏。
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    int tag = [self.button tag];

    UIButton *btn = (UIButton *) [self.view viewWithTag:1001];
    btn.titleLabel.text = @"hello world";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

上面是代码,现在出现的问题是: int tag = [self.button tag];返回了tag的值,我设置的是1001。

但是 UIButton *btn = (UIButton *) [self.view viewWithTag:1001];返回的却是nil。

我搜了下google,也没看到有人解决了这个问题。 http://84.15.64.35/#newwindow=1&q=self.view+viewWithTag+return+nil

所以,在这里请教下大家了。

共2条回复
龙凡31 回复于 2015年04月13日

for (UIView *v in self.view.subviews) { NSLog("%d", (int)v.tag; }

看看到底有没有 1001这个...

Hector 回复于 2015年04月13日

可以用楼上的方法检查下

可能 button 的父 view 不是 self.view

登录 或者 注册
相关帖子