2014-09-11 49 views
0

我有一個UIToolbarButton,它有一個UIButton來保存圖像。現在,當我點擊這個UIToolbarButton時,我想在當前視圖中打開另一個視圖。UIToolbarButton上的TapGestureRecognizer不起作用

我的代碼:

- (IBAction)btnNowPlaying:(id)sender 
{ 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self 
            action:@selector(openPlaying:)]; 
    [self.view addGestureRecognizer:tap]; 
} 

- (void)openPlaying:(UIGestureRecognizer *)sender 
{ 
    NewMainViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Center"]; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UIViewController * npvc = [storyboard instantiateViewControllerWithIdentifier:@"NowPlayingVC"]; 
    [vc.containerView addSubview:npvc.view]; 
} 

這個動作,btnNowPlaying是對的UIButton。

+0

爲什麼不直接使用UIBarButton項目的圖像屬性,而不是將UIButton放置在酒吧按鈕內? – trevorj 2014-09-11 05:51:47

+0

如果我使用圖像屬性,圖像未顯示 – 2014-09-11 05:53:39

+0

只顯示色調顏色 – 2014-09-11 05:55:04

回答

1

這聽起來像你只是想從一個視圖控制器導航到另一個。只需將控件從您的酒吧按​​鈕項拖動到您想要在故事板中導航的視圖控制器即可。然後,單擊兩個視圖控制器之間的segue,並在Attributes檢查器中設置它的標識符。在此之後,所有你需要做的是落實prepareForSegue:

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"THE_IDENTIFIER_YOU_ENTERED_IN_THE_ATTRIBUTES_INSPECTOR"]) { 
     if ([segue.destinationViewController isKindOfClass:[NEWViewController class]]) { 
      NEWViewController *vc = (NEWViewController *)segue.destinationViewController; 
      // Any preparation you need to do for next view controller 
      vc.someProperty = someValue; 
     } 
    } 
} 

這是一個有點陌生,打開當前視圖控制器內一個新的視圖控制器。爲什麼不直接繼續呢?

另一種選擇是alloc-init一個UIView,而不是視圖控制器,只是addSubview:

0

首先,您可以使用默認的UIBarButton項目和Custom樣式和設置圖片屬性,而不是創建UIButton

您不需要創建額外的手勢識別器。如果在Interface Builder中將IBAction與按鈕連接起來,則可以將轉換代碼放入btnNowPlaying方法中。

另一種方法是創建IBOutlet,將其與您的按鈕連接並在- (void)viewDidLoad方法中創建UIGestureRecognizer。

+0

嘗試....沒有運氣 – 2014-09-11 06:12:00

+0

- (void)viewDidLoad { [super viewDidLoad]; UIImage * i = [UIImage imageNamed:@「1.png」]; CGSize destinationSize = CGSizeMake(25,25); UIGraphicsBeginImageContext(destinationSize); [i drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); btnNewImage.image = newImage; // btnNewImage.tintColor = [UIColor clearColor]; } – 2014-09-11 06:14:35

+0

任何其他方式來解決這個問題? – 2014-09-11 06:33:17