簡體   English   中英

UIBarButtonItem 不可點擊

[英]UIBarButtonItem not clickable

我有一個非常棘手的問題,經過長時間的搜索(谷歌,stackoverflow,...)我沒有得到適合我的解決方案。

讓我介紹一下我目前選擇的架構:

  1. 我有一個 AppDelegate,它有一個 UIView,其中包含一個 UINavigationController 和應用程序 didFinishLaunchingWithOptions: 包含:

     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. 在我的 FSCScrumRootView(從 UIViewController 繼承)中,我像這樣初始化視圖:

     - (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. 在我的 FSCScrumProjectListView(它繼承自 UITableViewController)中,我實現了 viewDidLoad,如下所示:

     - (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. 這最終會出現在以下屏幕中(就像我想要的那樣):查看當前結果的 iOS Mockup

問題:我現在的問題是,我只能點擊刷新按鈕。 無法單擊其他兩個按鈕(信息和注銷)。 我不明白為什么? 我在這里做錯了什么?

非常感謝您的幫助!

嘗試像第一個按鈕(刷新)一樣自動釋放后兩個按鈕。

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

伙計們,我找到了問題的原因,我不得不說,這是一個非常非常愚蠢的問題。

這個項目的第一行負責所有的麻煩:

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

也許有更好的解決方案來獲取動態屏幕尺寸?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM