视图弹出后如何从uinavigationcontroller 中删除segmentedcontroller?

发布于 2024-09-01 13:58:38 字数 857 浏览 2 评论 0 原文

我正在 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。够了吗?

I'm building a segmented control within my viewDidLoad method, like so:

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];

But when the user goes Back in the uinavigationcontroller hierarchy, the segmented controller stays on the navigation bar. How would I get rid of it? Or am I doing something fundamentally wrong?


EDIT

Following Alex's suggestions, I propertized tabs and tried:

  NSArray *tabItems = [NSArray arrayWithObjects:@"FAQs", @"Terms", nil];
  self.tabs = [[UISegmentedControl alloc] initWithItems:tabItems];

but I'm not sure it's a good idea to alloc a property;

And I'm using

  [self.tabs removeFromSuperview];

in my viewWillDisappear. Is that enough?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流绪微梦 2024-09-08 13:58:38

在视图控制器中保留对分段控件的引用(即将选项卡定义为视图控制器头文件中的属性)。

覆盖视图控制器的 -viewWillDisappear: 方法,并使用该控件的 -viewWillDisappear: 方法从导航栏中删除分段控件。 com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCBEIDB" rel="nofollow noreferrer">-removeFromSuperview 方法。

编辑

您仍然需要在-viewDidLoadalloc-init分段控件tabs。您只需在视图控制器的标头中为 tabs 设置保留属性,并将控件的 release 语句移至视图控制器的 dealloc 方法。

请阅读此Objective-C 教程的“属性”部分,了解属性以及如何设置的介绍他们起来。

重写方法的方法如下:

- (ReturnClass) methodNameToOverride:args {
    [super methodNameToOverride:args];
    // your code goes here...
}

对于 -viewWillDisappear: 来说,当视图控制器即将消失时,例如当它从导航堆栈中弹出时,会调用此方法。这是放置管理特定于视图控制器的项目(例如分段控件)清理的代码的好地方:

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [tabs removeFromSuperview];
}

编辑2

如果您的属性设置如下:

@property (nonatomic, retain) UISegmentedControl *tabs;

那么您将保留您设置self.tabs等于的任何内容。

您此处的代码:

self.tabs = [[UISegmentedControl alloc] initWithItems:...];

将造成内存泄漏,因为您保留了此对象:[[UISegmentedControl alloc] init] - 但您从未释放[[UISegmentedControl alloc] init]本身。这很糟糕。

相反,请在右侧使用autorelease,即:

self.tabs = [[[UISegmentedControl alloc] initWithItems:...] 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 control tabs in -viewDidLoad. You just need to set up a retain property for tabs in your view controller's header, and move the control's release statement to the view controller's dealloc 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:

- (ReturnClass) methodNameToOverride:args {
    [super methodNameToOverride:args];
    // your code goes here...
}

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:

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [tabs removeFromSuperview];
}

EDIT 2

If your property is set as follows:

@property (nonatomic, retain) UISegmentedControl *tabs;

then you are going to retain anything you set self.tabs equal to.

Your code here:

self.tabs = [[UISegmentedControl alloc] initWithItems:...];

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.:

self.tabs = [[[UISegmentedControl alloc] initWithItems:...] autorelease];

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.

腻橙味 2024-09-08 13:58:38

更好的是,将 UISegmentedControl 所属的 UIViewController 的 navigationItem.titleView 设置为 UISegmentedControl。

-(void) viewDidLoad {
    if(!mySegmentedControl) {
        // initialize the UISegmentedControl
        ...
    }

    self.navigationItem.titleView = mySegmentedControl; 
}

不需要手动删除 UISegmentedControl 或任何其他 UIView ,当然,除了在拥有 UIViewController 被释放时释放。另外,为了成为一个“良好记忆力的公民”,您可以在 -viewDidUnload 中将您的属性设置为 nil。

您还可以自定义 UIViewController 的 navgiationItem 的左右 barButtonItems。

Even better, set the UISegmentedControl's owning UIViewController's navigationItem.titleView to the UISegmentedControl.

-(void) viewDidLoad {
    if(!mySegmentedControl) {
        // initialize the UISegmentedControl
        ...
    }

    self.navigationItem.titleView = mySegmentedControl; 
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文