我有一个自定义类(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
在这里你触发你的按钮动作..只要把你的代码触发你的按钮动作。当你点击你的单元格时,你需要UIPopoverController ..我是正确的。 – TamilKing
我不知道我跟着你。 IBAction方法位于FFFuelQuantityPickerVC视图控制器中。 self.poc _is_是UIPopoverController,通过更改其contentViewController来重用。 – MTT727