2013-10-25 73 views
0

我有一個自定義類(FFFuelQuantityPickerVC)故事板視圖控制器包含UIPickerView和UIButton。 UIButton連接到FFFuelQuantityPickerVC中稱爲fullButtonPressed的IBAction。當這個視圖控制器使用storyboard popover segue來呈現時,當UIButton被「觸摸到內部」時,fullButtonPressed IBAction被觸發。爲什麼不是我的IBAction射擊?

但是,當我以編程方式初始化並在彈出窗口中顯示FFFuelQuantityPickerVC時,按下UIButton不會觸發IBAction。爲什麼會這樣?這裏是按下UITableViewCell中的按鈕時執行編程演示的代碼。 (self.poc是可重複使用的酥料餅控制器):

-(void)thisHelperPressed:(UIButton *)thisHelper inCell:(UITableViewCell *)thisCell{ 

    if ([thisHelper.titleLabel.text isEqualToString:@"FuelPicker"]){ 
     //init the fuel quantity picker VC 
     FFFuelQuantityPickerVC *fuelQuantityPickerVC; 
     fuelQuantityPickerVC =[[UIStoryboard storyboardWithName:@"MainStoryboard" 
                 bundle:nil] 
         instantiateViewControllerWithIdentifier:@"FuelQuantityPickerVC"]; 
     fuelQuantityPickerVC.delegate = self; 
     [self.poc setContentViewController:fuelQuantityPickerVC]; 
    } else if... 
    } 
    self.poc.delegate = self; 

    //present it 
    [self.poc presentPopoverFromRect:thisHelper.frame inView:thisCell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    .  
    . 
    . 

這裏是FFFuelQuantityPickerVC:

// FFFuelQuantityPickerVC.h 
#import <UIKit/UIKit.h> 

@protocol fuelPickerDelegate <NSObject> 
-(void) fuelQuantityChangedL:(NSInteger) quantityL R:(NSInteger)quantityR; 
@end 

@interface FFFuelQuantityPickerVC : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> 
@property (weak, nonatomic) IBOutlet UIPickerView *thisPicker; 
@property (nonatomic) NSString *fuelQuantityL; 
@property (nonatomic) NSString *fuelQuantityR; 

@property (nonatomic) id delegate; 

- (IBAction)fullButtonPressed; //this is wired to the button 

@end 

// 
// FFFuelQuantityPickerVC.m 
#import "FFFuelQuantityPickerVC.h" 

#define FUEL_MIN 0 
#define FUEL_MAX 146 
#define LEFT_COMPONENT 0 
#define RIGHT_COMPONENT 1 

@interface FFFuelQuantityPickerVC() 

@end 

@implementation FFFuelQuantityPickerVC 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    if (!self.fuelQuantityL){ 
    self.fuelQuantityL = [NSString stringWithFormat:@"%i", FUEL_MAX]; 
    } 
    if (!self.fuelQuantityR){ 
     self.fuelQuantityR = [NSString stringWithFormat:@"%i", FUEL_MAX]; 
    } 
} 

//set selected row to current values, if any 
- (void) viewDidAppear:(BOOL)animated { 
     [self.thisPicker selectRow:FUEL_MAX - [self.fuelQuantityL intValue] inComponent:LEFT_COMPONENT animated:YES]; 
     [self.thisPicker selectRow:FUEL_MAX - [self.fuelQuantityR intValue] inComponent:RIGHT_COMPONENT animated:YES]; 

} 

//this method does not get called when the button is pressed (except when the VC is presented via storyboard popover segue) 
- (IBAction)fullButtonPressed { 
    self.fuelQuantityL = [NSString stringWithFormat:@"%i", FUEL_MAX]; 
    self.fuelQuantityR = [NSString stringWithFormat:@"%i", FUEL_MAX]; 
    [self.thisPicker selectRow:0 inComponent:0 animated:YES]; 
    [self.thisPicker selectRow:0 inComponent:1 animated:YES]; 
    [self.delegate fuelQuantityChangedL:[self.fuelQuantityL integerValue] 
             R:[self.fuelQuantityR integerValue]]; 

} 

#pragma mark - PickerViewDataSource delegate methods 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 2; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return (FUEL_MAX-FUEL_MIN + 1); 
} 

#pragma mark - PickerView delegate methods 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ 
    float myWidth = self.view.frame.size.width; 
if (component == 0) return myWidth/3; 
return (myWidth * 2/3); 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { 
    //TODO 
    return 28; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [NSString stringWithFormat:@"%d", (FUEL_MAX)-row]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    if (component == LEFT_COMPONENT){ 
     self.fuelQuantityL = [NSString stringWithFormat:@"%i", FUEL_MAX - row]; 
    } else { 
     self.fuelQuantityR = [NSString stringWithFormat:@"%i", FUEL_MAX - row]; 
    } 
    [self.delegate fuelQuantityChangedL:[self.fuelQuantityL integerValue] 
             R:[self.fuelQuantityR integerValue]]; 
} 


@end 
+0

在這裏你觸發你的按鈕動作..只要把你的代碼觸發你的按鈕動作。當你點擊你的單元格時,你需要UIPopoverController ..我是正確的。 – TamilKing

+0

我不知道我跟着你。 IBAction方法位於FFFuelQuantityPickerVC視圖控制器中。 self.poc _is_是UIPopoverController,通過更改其contentViewController來重用。 – MTT727

回答

0

這是我的代碼。我米使用此代碼顯示UIPopoverController當我點擊單元格並從挑值UIPickerView的UITableViewCell

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     if (indexPath.row == 1) 
     { 
      //PopoverView controller......................................................................... 
      UIViewController *popoverViewController = [[UIViewController alloc] init]; 

      //PopoverView.................................................................................... 
      UIView *popoverView = [[UIView alloc]init]; 
      [popoverView setBackgroundColor:[UIColor blackColor]]; 

      //Navigation bar and Barbutton items.............................................................. 
      UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
      navBar.barStyle = UIBarStyleBlackTranslucent; 

      UIBarButtonItem *btnDone = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doDone)]; 
      UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(doCancel)]; 

      UINavigationItem *navItem = [[UINavigationItem alloc]init]; 
      navItem.rightBarButtonItem = btnDone; 
      navItem.leftBarButtonItem = btnCancel; 

      navBar.items = [[NSArray alloc]initWithObjects:navItem, nil]; 
      [popoverView addSubview:navBar]; 

      //UIPickerView..................................................................................... 
      UIPickerView *sortPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 180)]; 
      popoverViewController.contentSizeForViewInPopover = CGSizeMake(320, 230); 
      sortPickerView.delegate = self; 
      sortPickerView.dataSource = self; 
      sortPickerView.showsSelectionIndicator = YES; 
      [popoverView addSubview:sortPickerView]; 
      NSUserDefaults *ud1 = [NSUserDefaults standardUserDefaults]; 

      if ([ud1 objectForKey:@"languages"]) 
      { 
       NSUInteger row1 = [languages indexOfObject:[ud1 objectForKey:@"languages"]]; 
       if (row1) 
       { 
        [sortPickerView selectRow:row1 inComponent:0 animated:YES]; 
       } 
      } 
      else 
      { 
       [sortPickerView selectRow:0 inComponent:0 animated:YES]; 
      } 

      CGRect popFrame = lbl_language.frame; 
      popFrame.origin.y = popFrame.origin.y + popFrame.size.height + 70; 
      popFrame.origin.x = popFrame.origin.x + 100; 
      popoverViewController.view = popoverView; 
      self.SortPopover = [[UIPopoverController alloc] initWithContentViewController:popoverViewController]; 
      [self.SortPopover presentPopoverFromRect:popFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 
     } 
    } 
+0

我的ViewController的選擇器部分工作正常 - 按預期調用委託方法pickerView:didSelectRow:inComponent:。這是在ViewController視圖中連接到我的UIButton的IBAction方法,它沒有被觸發。 – MTT727

+0

請放在你觸發按鈕的地方。 – TamilKing

+0

我已將VC代碼添加到原始問題中。 – MTT727

0

來顯示的UILabel的價值對於那些我不清楚原因, UI Button沒有收到推送 - 它從未突出顯示。它沒有被我能看到的另一種觀點覆蓋(唯一的另一種觀點是選擇器)。無論如何,我添加了一個UIToolbar,並將按鈕更改爲條形按鈕項目,並且工作正常。

相關問題