UIBarButtonItem 不可点击

发布于 2024-11-04 06:20:06 字数 2982 浏览 0 评论 0原文

我有一个非常棘手的问题,经过长时间的搜索(谷歌,stackoverflow,...)我没有得到适合我的解决方案。

让我向您介绍我当前选择的架构:

  1. 我有一个 AppDelegate,它有一个包含 UINavigationController 的 UIView 和应用程序 didFinishLaunchingWithOptions: contains:

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
      UIViewController *myController = [[UIViewController alloc] init];
      myController.view = myView;
    
    
          FSCScrumRootView * myRootView = [[FSCScrumRootView alloc] initWithNibName:@"FSCScrumRootView" 包:[NSBundle mainBundle]];
    
          [myController.view addSubview:myRootView.navigation.view];
    
          [self.window addSubview:myController.view];
    
          [self.window makeKeyAndVisible];
          返回是;
        }
    
  2. 在我的 FSCScrumRootView (继承自 UIViewController)中,我像这样初始化视图:

    - (id)initWithNibName:(NSString *)nibNameOrNil 捆绑包:(NSBundle *)nibBundleOrNil 
    { 
    self = [超级 initWithNibName:nibNameOrNil 捆绑:nibBundleOrNil];
    
    如果(自我){
    // 自定义初始化
    self.navigation = [[[UINavigationController alloc] init] autorelease];
    self.scrumProjectsList = [[[FSCScrumProjectListView alloc] init] initWithNibName:@"FSCScrumProjectListView" 包:nil];
    [导航pushViewController:scrumProjectsList动画:是]; 
    
    [导航视图]; 
    } 
    返回自我; 
    } 
    
  3. 在我的 FSCScrumProjectListView (它继承自 UITableViewController)中,我实现了 viewDidLoad,如下所示:

    - (void)viewDidLoad 
    { 
    [超级viewDidLoad]; 
    
    //设置标题 
    self.navigationItem.title = @"Scrum 项目";
    
    UIBarButtonItem *myRefreshButton = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonSystemItemRefresh target:self action:@selector(refreshList)] autorelease]; 
    self.navigationItem.leftBarButtonItem = myRefreshButton;
    
    UIBarButtonItem *myLogoutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonSystemItemCancel target:self action:@selector(logout)]; 
    self.navigationItem.rightBarButtonItem = myLogoutButton;
    
    
    //初始化工具栏
    工具栏= [[UIToolbar分配]初始化];
    工具栏.barStyle = UIBarStyleDefault;
    
    //设置工具栏以适合应用程序的宽度。
    [工具栏大小适合];
    
    //计算工具栏的高度
    CGFloattoolbarHeight = [工具栏框架].size.height;
    
    //获取父视图的边界
    CGRect rootViewBounds = self.parentViewController.view.bounds;
    
    //获取父视图的高度。
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
    
    //获取父视图的宽度,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
    
    //为工具栏创建一个矩形
    CGRect rectArea = CGRectMake(0, rootViewHeight - 工具栏高度, rootViewWidth, 工具栏高度);
    
    //重新定位接收器并调整其大小
    [工具栏setFrame:rectArea];
    
    //创建一个按钮
    UIBarButtonItem *infoButton = [[UIBarButtonItem 分配] 
                                 initWithTitle:@"Info" 样式:UIBarButtonItemStyleBordered 目标:self 操作:@selector(info_clicked:)];
    
    [工具栏 setItems:[NSArray arrayWithObjects:infoButton,nil]];
    
    //将工具栏作为子视图添加到导航控制器中。
    [self.navigationController.view addSubview:工具栏];
    
    //重新加载表视图
    [self.tableView reloadData]; 
    }
    
  4. 这最终导致以下屏幕(正如我想要的那样): 查看当前结果的 iOS 模型

问题: 我现在的问题是,我只能单击刷新按钮。其他两个按钮(信息和注销)无法单击。我不明白为什么?我在这里做错了什么?

非常感谢您的帮助!

I have a very tricky problem and after long searching (google, stackoverflow, ...) i didn't get the solution that works for me.

Let me introduce you in my current choosen architecture:

  1. I have a a AppDelegate, which has a UIView that contains a UINavigationController and the application didFinishLaunchingWithOptions: contains:

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
      UIViewController *myController = [[UIViewController alloc] init];
      myController.view = myView;
    
    
          FSCScrumRootView * myRootView = [[FSCScrumRootView alloc] initWithNibName:@"FSCScrumRootView" bundle:[NSBundle mainBundle]];
    
          [myController.view addSubview:myRootView.navigation.view];
    
          [self.window addSubview:myController.view];
    
          [self.window makeKeyAndVisible];
          return YES;
        }
    
  2. In my FSCScrumRootView (inherits from UIViewController) i init the view like this:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
    // Custom initialization
    self.navigation = [[[UINavigationController alloc] init] autorelease];
    self.scrumProjectsList = [[[FSCScrumProjectListView alloc] init] initWithNibName:@"FSCScrumProjectListView" bundle:nil];
    [navigation pushViewController:scrumProjectsList animated:YES]; 
    
    [navigation view]; 
    } 
    return self; 
    } 
    
  3. In my FSCScrumProjectListView (it is inherited from UITableViewController) i have implemented the viewDidLoad as following:

    - (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    
    //Set the title 
    self.navigationItem.title = @"Scrum Projects";
    
    UIBarButtonItem *myRefreshButton = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonSystemItemRefresh target:self action:@selector(refreshList)] autorelease]; 
    self.navigationItem.leftBarButtonItem = myRefreshButton;
    
    UIBarButtonItem *myLogoutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonSystemItemCancel target:self action:@selector(logout)]; 
    self.navigationItem.rightBarButtonItem = myLogoutButton;
    
    
    //Initialize the toolbar
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    
    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];
    
    //Caclulate the height of the toolbar
    CGFloat toolbarHeight = [toolbar frame].size.height;
    
    //Get the bounds of the parent view
    CGRect rootViewBounds = self.parentViewController.view.bounds;
    
    //Get the height of the parent view.
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
    
    //Get the width of the parent view,
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
    
    //Create a rectangle for the toolbar
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
    
    //Reposition and resize the receiver
    [toolbar setFrame:rectArea];
    
    //Create a button
    UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] 
                                 initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
    
    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
    
    //Add the toolbar as a subview to the navigation controller.
    [self.navigationController.view addSubview:toolbar];
    
    //Reload the table view
    [self.tableView reloadData]; 
    }
    
  4. This results finally in the following screen (as i'd like to have it):
    View iOS Mockup of current result

The Problem:
My Problem now is, that i can click ONLY on the Refresh Button. The other two buttons (Info and Logout) cannot be clicked. And i don't understand why? What I am doing wrong here?

Your help is kindly appreceated!

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

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

发布评论

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

评论(2

不及他 2024-11-11 06:20:06

尝试像第一个按钮(刷新)一样自动释放后两个按钮。

UIBarButtonItem *myLogoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonSystemItemCancel target:self action:@selector(logout)]autorelease];



UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] 
                             initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]autorelease];

Try autoreleasing the second two buttons like your first one (the refresh).

UIBarButtonItem *myLogoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonSystemItemCancel target:self action:@selector(logout)]autorelease];



UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] 
                             initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)]autorelease];
焚却相思 2024-11-11 06:20:06

各位,我已经找到了问题的原因,我不得不说,这是一个非常非常愚蠢的问题。

该项目的第一行负责所有麻烦:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];

由于我创建的视图的大小只有 200x400,因此它太小,无法识别出现在该视图外部的任何事件(尽管一切都是可见的)。

如果我更改此视图的大小,一切都会按预期进行:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];

但更动态的解决方案可能是:

CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cgSize.width, cgSize.height)];

也许有更好的解决方案来获取动态屏幕尺寸?

People, i have found the reason for my problem and i have to say, it was a very very silly one.

The first line of this project was responsible for all the troubels:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];

Since the View i created there is only sized 200x400, it is to small to recognize any events that appear in the outside of this view (although everything is visible).

If i change the size of this view, all works as expected:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];

But the more dynamic solution would probably be:

CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cgSize.width, cgSize.height)];

Maybe there is an even better solution for getting the dynamic screen size?

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