按钮单击以编程方式切换UIViewControllers

时间:2014-07-09 12:40:02

标签: ios xcode uiviewcontroller uibutton

我有2个viewcontrollers。起始/第一个是通过故事板制作的,而另一个是通过编程方式制作的。第二个包含工具栏和表格。我在第二个按钮上做了一个按钮,当点击它时,应用程序应该返回到主视图控制器。该按钮放置在tableview的一行中,具有自定义单元格样式(imageCellCell),该样式具有自己的类。到目前为止我已尝试过这个但是我收到以下错误:

imageCellCell.h

#import <UIKit/UIKit.h>

@interface imageCellCell : UITableViewCell

@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UIButton *homebtn;



@end

imageCellCell.m

#import "imageCellCell.h"

@implementation imageCellCell

@synthesize view;
@synthesize label1;
@synthesize label2;
@synthesize prodimage;
@synthesize thumbsup;
@synthesize label3;
@synthesize basket;
@synthesize home;
@synthesize homebtn;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        view = [[UIView alloc] initWithFrame:self.frame];
        [self addSubview:view];

        // initiate home button       
        homebtn = [[UIButton alloc]initWithFrame:CGRectMake(4,0, 30, 30)];

        [homebtn setTintColor:[UIColor clearColor]]; 
        [homebtn setBackgroundImage:[UIImage imageNamed:@"home.jpg"]
                            forState:UIControlStateNormal];

        [homebtn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];


        [view addSubview:homebtn];

    }
    return self;
}

@end

secondviewcontroller.h

#import <UIKit/UIKit.h>

@interface secondviewcontroller.h : UIViewController <UITableViewDelegate , UITableViewDataSource>
{ NSArray* data;
}

@property (strong, nonatomic) IBOutlet UITableView *tableview;


@property (strong, nonatomic) IBOutlet UITabBar *tabbar;


-(IBAction)clickButton:(id)sender;


@end

secondviewcontroller.m

#import "secondviewcontroller.h"
#import "imageCellCell.h"


@interface secondviewcontroller ()

@end

@implementation secondviewcontroller
@synthesize tableview;
@synthesize tabbar;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {





NSString *CellIdentifier;
NSString *CellIdentifierimg;

    UITableViewCell *cell;
    if (cell == nil) {
        if (indexPath.row == 0) {
            cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
        } else if (indexPath.row == 1) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
    }
    }





    if (indexPath.row == 1) {

    } else if (indexPath.row == 2) {

    }

    switch ([indexPath row])
    {
        case 0:
        {

            imageCellCell *firstRowCell = (imageCellCell *)cell;


            firstRowCell.accessoryType = UITableViewCellAccessoryNone;

            cell.selectionStyle = UITableViewCellSelectionStyleNone;


            break;


        }
        case 1:
        {

                cell.textLabel.text = @"Detailed Score";
                cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;


            break;

        }



-(IBAction)clickButton:(id)sender{

    NSString * storyboardName = @"MainStoryboard"; 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"maincontroller"];
    [self presentViewController:vc animated:YES completion:nil];    
}


@end

错误:

   014-07-09 14:23:16.022 EcoUI[1046:f803] -[imageCellCell clickButton:]: unrecognized selector sent to instance 0x6888bd0
    2014-07-09 14:23:16.026 EcoUI[1046:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[imageCellCell clickButton:]: unrecognized selector sent to instance 0x6888bd0'

更新了 imageCellCell.m

中的代码
#import "SecondViewController.h"


     SecondViewController *controltarg = [[SecondViewController alloc] init];
            [homebtn addTarget:controltarg action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

2 个答案:

答案 0 :(得分:1)

在按钮选择器目标的imageCellCell.m文件中,您已分配self,这意味着它将查看imageCellCell.m文件中的选择器方法,但它不存在。

因此它给出错误,作为无法识别的选择器。

答案 1 :(得分:0)

按钮,您可以在tableview单元格中使用它,并在那里添加该按钮的操作。但是你在视图控制器中给出了按钮动作的实现。这就是为什么你得到那个错误。尝试使用委托。