UIBarButtonItem не активируется - PullRequest
1 голос
/ 29 апреля 2011

У меня очень сложная проблема, и после долгих поисков (google, 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-макета текущего результата

Проблема: Моя проблема сейчас в том, что я могу нажать ТОЛЬКО на кнопку «Обновить». Две другие кнопки (информация и выход) не могут быть нажаты. И я не понимаю, почему? Что я тут не так делаю?

Ваша помощь приветствуется!

Ответы [ 2 ]

0 голосов
/ 02 мая 2011

Люди, я нашел причину своей проблемы, и я должен сказать, что она была очень очень глупой.

Первая строка этого проекта была ответственна за все проблемы:

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

Поскольку созданный мной вид имеет размер только 200х400, он слишком мал, чтобы распознавать любые события, которые появляются за пределами этого вида (хотя все видно).

Если я изменяю размерс этой точки зрения все работает, как ожидалось:

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

Может быть, есть еще лучшее решение для получения динамического размера экрана?

0 голосов
/ 29 апреля 2011

Попробуйте автоматически освободить вторые две кнопки, например, первую (обновление).

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