视图弹出后如何从uinavigationcontroller 中删除segmentedcontroller?
我正在 viewDidLoad 方法中构建分段控件,如下所示:
NSArray *tabitems = [NSArray arrayWithObjects:@"ONE", @"TWO", nil];
UISegmentedControl *tabs = [[UISegmentedControl alloc] initWithItems:tabitems];
tabs.segmentedControlStyle = UISegmentedControlStyleBar;
tabs.frame = CGRectMake(185.0, 7.0, 130.0, 30.0);
tabs.selectedSegmentIndex = 0;
[self.navigationController.navigationBar addSubview:tabs];
[tabs release];
但是当用户返回 uinavigationcontroller 层次结构时,分段控制器仍保留在导航栏上。我该如何摆脱它?或者我做的事情根本就是错误的吗?
编辑
按照亚历克斯的建议,我对选项卡进行了属性化并尝试了:
NSArray *tabItems = [NSArray arrayWithObjects:@"FAQs", @"Terms", nil];
self.tabs = [[UISegmentedControl alloc] initWithItems:tabItems];
但我不确定分配属性是个好主意;
我
[self.tabs removeFromSuperview];
在我的视图中使用WillDisappear。够了吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在视图控制器中保留对分段控件的引用(即将选项卡定义为视图控制器头文件中的属性)。
覆盖视图控制器的
-viewWillDisappear:
方法,并使用该控件的-viewWillDisappear:
方法从导航栏中删除分段控件。 com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCBEIDB" rel="nofollow noreferrer">-removeFromSuperview
方法。编辑
您仍然需要在
-viewDidLoad
中alloc-init
分段控件tabs
。您只需在视图控制器的标头中为tabs
设置保留属性,并将控件的release
语句移至视图控制器的dealloc
方法。请阅读此Objective-C 教程的“属性”部分,了解属性以及如何设置的介绍他们起来。
重写方法的方法如下:
对于
-viewWillDisappear:
来说,当视图控制器即将消失时,例如当它从导航堆栈中弹出时,会调用此方法。这是放置管理特定于视图控制器的项目(例如分段控件)清理的代码的好地方:编辑2
如果您的属性设置如下:
那么您将
保留
您设置self.tabs
等于的任何内容。您此处的代码:
将造成内存泄漏,因为您保留了此对象:
[[UISegmentedControl alloc] init]
- 但您从未释放[[UISegmentedControl alloc] init]
本身。这很糟糕。相反,请在右侧使用
autorelease
,即:tabs
属性保留其自己对初始化分段控件的引用。初始化的分段控制本身会在稍后的某个时刻正确释放。所以不再有内存泄漏。Retain a reference to the segmented control in your view controller (i.e define
tabs
as a property in the view controller's header file).Override the view controller's
-viewWillDisappear:
method, and remove the segmented control from the navigation bar there, using the control's-removeFromSuperview
method.EDIT
You would still
alloc-init
your segmented controltabs
in-viewDidLoad
. You just need to set up a retain property fortabs
in your view controller's header, and move the control'srelease
statement to the view controller'sdealloc
method.Read the "Properties" section of this Objective-C tutorial for an introduction to properties and how to set them up.
The way to override a method is as follows:
In the case of
-viewWillDisappear:
, this method gets called when your view controller is about to disappear, such as when it gets popped off the navigation stack. This is a great place to put code that manages clean-up of view-controller-specific items, like your segmented control:EDIT 2
If your property is set as follows:
then you are going to
retain
anything you setself.tabs
equal to.Your code here:
will create a memory leak, because you are retaining this object:
[[UISegmentedControl alloc] init]
— but you never release[[UISegmentedControl alloc] init]
itself. This is bad.Instead, use
autorelease
on the right side, i.e.:The
tabs
property retains its own reference to the initialized segmented control. That initialized segmented control is itself released properly at some later point. So no more memory leak.更好的是,将 UISegmentedControl 所属的 UIViewController 的 navigationItem.titleView 设置为 UISegmentedControl。
不需要手动删除 UISegmentedControl 或任何其他 UIView ,当然,除了在拥有 UIViewController 被释放时释放。另外,为了成为一个“良好记忆力的公民”,您可以在
-viewDidUnload
中将您的属性设置为 nil。您还可以自定义 UIViewController 的 navgiationItem 的左右 barButtonItems。
Even better, set the UISegmentedControl's owning UIViewController's navigationItem.titleView to the UISegmentedControl.
No manual removal of the UISegmentedControl or any other UIView for that matter required except, of course, releasing when owning UIViewController is dealloc'ed. Also, to be a "good memory citizen", you might set your property to nil in
-viewDidUnload
.You can also customize the left and right barButtonItems of the UIViewController's navgiationItem.