objective-c - 如何从导航栏中删除按钮?

标签 objective-c ios ipad

我一直在做ipad应用。在这个应用程序中我有几个观点。这是流程

欢迎屏幕 > 主屏幕 > 其余屏幕

我在所有屏幕的导航栏上应用了主页图标(按钮)。在任何屏幕上按下主页图标都会将用户带到主屏幕。我在Home类的viewDidLoad中写了如下代码

//**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];

homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

这个按钮是功能性的。 goHome 是@selector 中应用的方法的名称。 我想从主屏幕上删除此按钮并将其保留在其余屏幕上。我已经应用了几件事,但我不知道该怎么做。这看起来很简单,但我还是不明白。请指导..

问候 电脑

最佳答案

在您的“主屏幕”viewDidAppear 方法中执行此操作:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
     if(view.tag == 10)
     {
        view.hidden = YES;
     }
}

在您创建按钮的其他 View Controller 中,将主页按钮的标签设置为 10。

/**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homebutton setTag:10]; // Set tag to 10 or any value
homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

在你的 viewDidAppear: 中,另一个 View Controller:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
    if(view.tag == 10)
    {
      view.hidden = NO;
    }
}

关于objective-c - 如何从导航栏中删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008605/

相关文章:

ios - UITableView 在初始加载时未正确绘制单元格 - Swift

ios - 我可以通过 Perfect 在 Linux 上使用 Realm 吗?

ios - 从 AppDelegate 重定向到特定屏幕 : On tap Notification

ios - UItabbar 项目不显示 Storyboard 引用

objective-c - 空 NSDatePicker

iphone - 我如何感知用户将手指按住屏幕而不移动?

iphone - viewDidLoad 在 initWithFrame 之前被调用?

iphone - 如何使用陀螺仪使 "paintball"停留在增强现实应用程序中的位置?

ios - 使用 iOS 应用程序将照片存储在 mysql 数据库中

ios - 从 didFinishLaunchingWithOptions 终止应用程序