为什么我的IBAction没有解雇?

时间:2013-10-25 03:35:34

标签: ios ibaction

我有一个包含UIPickerView和UIButton的自定义类(FFFuelQuantityPickerVC)Storyboard View Controller。 UIButton连接到FFFuelQuantityPickerVC中名为fullButtonPressed的IBAction。当使用故事板popover segue呈现此视图控制器时,当UIButton“内部触摸”时会触发fullButtonPressed 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

2 个答案:

答案 0 :(得分:0)

这是我的代码。当我单击单元格并从 UIPickerView 中选择值以在我的 UITableviewCell 中显示UILabel中的值时,我使用此代码显示 UIPopoverController

-(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];
        }
    }

答案 1 :(得分:0)

由于我不清楚的原因,UI按钮没有接收推送 - 它从未突出显示。它没有被我能看到的另一个视图覆盖(唯一的另一个视图是选择器)。无论如何,我添加了一个UIToolbar并将按钮更改为一个条形按钮项,它工作正常。