开发者

UIBarButtonItem not clickable

开发者 https://www.devze.com 2023-03-01 08:26 出处:网络
I have a very tricky problem and after long searching (google, stackoverflow, ...) i didn\'t get the solution that works for me.

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:UIBarButtonItemStyleBo开发者_C百科rdered 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!


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消