Ios 单击按钮以编程方式切换UIViewController

Ios 单击按钮以编程方式切换UIViewController,ios,xcode,uiviewcontroller,uibutton,Ios,Xcode,Uiviewcontroller,Uibutton,我有两个视图控制器。开始/第一个是通过故事板制作的,而另一个是通过编程制作的。第二个包含一个工具栏和一个表。我在第二个按钮中创建了一个按钮,当单击该按钮时,应用程序应返回到主视图控制器。按钮放置在具有自定义单元格样式(imageCellCell)的tableview的一行中,该样式有自己的类。到目前为止,我已尝试过此操作,但出现以下错误: imageCellCell.h #import <UIKit/UIKit.h> @interface imageCellCell : UITab

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

imageCellCell.h

#import <UIKit/UIKit.h>

@interface imageCellCell : UITableViewCell

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



@end
#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.h

#import <UIKit/UIKit.h>

@interface imageCellCell : UITableViewCell

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



@end
#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
错误:

   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 "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
#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
#import "SecondViewController.h"


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

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


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

按钮您将它放在tableview单元格中,并在那里为该按钮添加操作。但是,您在视图控制器中为按钮操作提供了实现,这就是为什么会出现这种错误。尝试使用委派。

你是对的。我在imageCellCell中包含了secondviewcontroller的targ,但它仍然无法切换视图控制器。它将给出输出(lldb),包括下面的新行。我试图创建buttons方法存在的类的新对象。将目标设置为其他类的正确方法是什么?但是我的按钮初始化位于不同的类中