gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:50:07 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于ios - 单击按钮以编程方式切换 UIViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24653961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com