2013-07-16 2 views
0

Я попытался добавить UIBarButtonItem к моему UINavigation бар, но я не могу получить его, чтобы показать:Как программно добавить кнопку для удаления WebView?

- (void)viewDidLoad { 
     [super viewDidLoad]; 

     self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height)]; 
     [self.view addSubview:self.webView]; 

     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
            initWithTitle:NSLocalizedString(@"Back", @"") 
            style:UIBarButtonItemStyleDone 
            target:self 
            action:@selector(remove:)]; 

     [self.navigationItem setLeftBarButtonItem: backButton]; 

     UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 


     [self.view addSubview:myBar]; 

     self.webView.delegate = self; 

     NSURLRequest *request = [NSURLRequest requestWithURL:self.url]; 
     [self.webView loadRequest:request]; 

    } 

    - (void)remove:(id)sender 
    { 
     [self removeFromParentViewController]; 
     [self.webView stopLoading]; 
     self.webView.delegate = nil; 
    } 

Я не могу понять, почему кнопка не существует. Любая помощь приветствуется!

Спасибо.

ответ

1

удалить эти две строки из кода и проверить его,

UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
     [self.view addSubview:myBar]; 

или вы можете добавить, как это,

UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];  
    [self.view addSubview:myBar]; 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"remove" 
            style:UIBarButtonItemStyleDone 
            target:self 
            action:@selector(remove:)]; 
    UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"remove"]; 
    [item setLeftBarButtonItem:backButton]; 
    [myBar setItems:[NSArray arrayWithObject:item]]; 
+0

@ Рихард Бертон: однажды проверьте мой обновленный ответ. – Balu

+0

Спасибо, Солнечный, очень полезно. –

2

Перед добавить UIBarButtonItem в UINavigationBar, первый изменить UIWebView рамку,

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height - 50)]; 

Здесь добавить (-50) до высоты UIWebView.

И теперь, я просто написать код, как добавить UIBarButtonItem к UINavigationBar:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    [self.view addSubview:navBar]; 
    UIBarButtonItem *Button = [[UIBarButtonItem alloc] 
           initWithTitle:@"MyButton" 
           style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(buttonAction)]; 

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"This is My Title "]; 
    item.rightBarButtonItem = Button; 
    item.hidesBackButton = YES; 
    [navBar pushNavigationItem:item animated:NO]; 

Вот имя метода buttonAction, что выполнение/вызова, когда вы нажали на UIBarButtonItem.

-(void)buttonAction 
{ 
    //stuff of code; 
} 
+0

Большое спасибо за указание вопроса о размере рамки. –

1

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

Случай 1: с помощью UINavigationController

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

Если вы используете NavigationController, тогда отобразите панель управления навигационного контроллера, и ваш код будет работать.

ниже двух линий не требуется.

UINavigationBar * myBar = [[UINavigationBar alloc] initWithFrame: CGRectMake (0, 0, 320, 50)]; [self.view addSubview: myBar];

Случай 2: Не используя UINavigationController

Если вы не используете UINavigationController, то вы должны добавить панель и добавить кнопку Бар в навигации пункт Бара.

UINavigationBar * myBar = [[UINavigationBar alloc] initWithFrame: CGRectMake (0, 0, 320, 50)]; [self.view addSubview: myBar];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"remove" 
           style:UIBarButtonItemStyleDone 
           target:self 
           action:@selector(remove:)]; 
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"remove"]; 
[item setLeftBarButtonItem:backButton]; 
[myBar setItems:[NSArray arrayWithObject:item]]; 

и вам необходимо удалить следующие строки

[self.navigationItem setLeftBarButtonItem:backButton]; 

Она будет работать отлично. Дайте мне знать, если вы найдете какие-либо проблемы.

+0

Спасибо, что нашли время ответить. Я использую UINavigationController. –

Смежные вопросы