切换视图控制器后添加标签
Adding tag after switching view controllers
我有一个随机生成的 int,我将其分配给 ViewWillAppear 中故事板上的 UIImageView 的标签。
然而,当我转到主菜单并尝试再次进入视图时,应用程序崩溃了。我知道这是因为标签,因为当我删除它时一切正常。
关于为什么它第一次有效但之后多次无效的任何想法?下面的代码。
ViewController.h:
@interface ViewController : UIViewController{
int tagnumber;
IBOutlet UIImageView *box;
...
}
ViewController.m:
-(void)viewWillAppear:(BOOL)animated{
tagnumber = arc4random()%1000;
box.tag = tagnumber;
...
}
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
[_animator removeAllBehaviors];
[box removeFromSuperview];
}
MainMenu.m:
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
}
基本上,当视图消失并且系统 运行 内存不足时,它会调用 UIViewController 的 didReceiveMemoryWarning
实现。这里有一个很好的描述:ViewWillDisappear versus dealloc
当你创建一个 IBOutput 时,你真的应该通过说 @property (weak, nonatomic) IBOutlet UIImageView *box
来有一个指向它的弱指针。视图保留它们的子视图,因此,如果一个视图被释放,它的子视图保留计数会减少一个。当视图消失时,系统会决定什么时候去分配self.view,当它去的时候,它会自动释放盒子,盒子也会被释放。你真的不需要担心。
我有一个随机生成的 int,我将其分配给 ViewWillAppear 中故事板上的 UIImageView 的标签。
然而,当我转到主菜单并尝试再次进入视图时,应用程序崩溃了。我知道这是因为标签,因为当我删除它时一切正常。
关于为什么它第一次有效但之后多次无效的任何想法?下面的代码。
ViewController.h:
@interface ViewController : UIViewController{
int tagnumber;
IBOutlet UIImageView *box;
...
}
ViewController.m:
-(void)viewWillAppear:(BOOL)animated{
tagnumber = arc4random()%1000;
box.tag = tagnumber;
...
}
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
[_animator removeAllBehaviors];
[box removeFromSuperview];
}
MainMenu.m:
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
}
基本上,当视图消失并且系统 运行 内存不足时,它会调用 UIViewController 的 didReceiveMemoryWarning
实现。这里有一个很好的描述:ViewWillDisappear versus dealloc
当你创建一个 IBOutput 时,你真的应该通过说 @property (weak, nonatomic) IBOutlet UIImageView *box
来有一个指向它的弱指针。视图保留它们的子视图,因此,如果一个视图被释放,它的子视图保留计数会减少一个。当视图消失时,系统会决定什么时候去分配self.view,当它去的时候,它会自动释放盒子,盒子也会被释放。你真的不需要担心。