我正在以编程方式创建UIButton。我希望UIViewController成为该按钮操作的目标。我该怎么做?。如果我点击按钮导航将无法工作。这是我的代码。请帮助我任何人。谢谢提前。
- (void)viewDidLoad
{
weeklyAudio=[UIButton buttonWithType:UIButtonTypeCustom];
[weeklyAudio addTarget:self action:@selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside];
[weeklyAudio setTitle:@"WEEKLY PREDICTIONS" forState:UIControlStateNormal];
weeklyAudio.titleLabel.textAlignment = NSLineBreakByWordWrapping;
weeklyAudio.titleLabel.numberOfLines = 2;
[weeklyAudio.titleLabel setTextAlignment: NSTextAlignmentCenter];
[weeklyAudio setTitleEdgeInsets:UIEdgeInsetsMake(60, 10, 10, 10)];
[weeklyAudio.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
weeklyAudio.frame=CGRectMake(10, 20, 140, 120);
weeklyAudio.layer.cornerRadius=5;
weeklyAudio.backgroundColor=[UIColor colorWithRed:232.0f/255.0f green:52.0f/255.0f blue:27.0f/255.0f alpha:1.0];
[backGroundImage addSubview:weeklyAudio];
weeklyAudioImage=[[UIImageView alloc]initWithFrame:CGRectMake(45, 10, 48, 48)];
weeklyAudioImage.image=[UIImage imageNamed:@"m48*48.png"];
[weeklyAudio addSubview:weeklyAudioImage];
weeklyAudio.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
}
-(void)weeklyPredictions
{
WeeklyViewController *weekly=[[WeeklyViewController alloc]init];
[self.navigationController pushViewController:weekly animated:NO];
}
答案 0 :(得分:0)
在-(void)weeklyPredictions
中设置断点,然后调试以查看单击按钮时是否在方法中运行。如果在其中运行,请在控制台中打印po [self navigationController]
以查看viewController是否具有导航控制器。如果结果为零,则无法推送另一个视图控制器。
答案 1 :(得分:0)
您是否使用导航控制器来推送和弹出viewcontrollers?如果不.. 使用
[self presentViewController:weekly animated:YES completion:nil];
展示你的下一个viewcontroller。
答案 2 :(得分:0)
在addSubview
[weeklyAudio addTarget:self action:@selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside];
将您的weeklyPredictions
方法修改为:
-(void)weeklyPredictions {
WeeklyViewController *weekly=[[WeeklyViewController alloc]initWithNibName:NSStringFromClass([WeeklyViewController class]) bundle:nil];
[self.navigationController pushViewController:weekly animated:NO];
}
答案 3 :(得分:0)
您需要在viewDidLoad薄荷中添加以下行:
backGroundImage.userInteractionEnabled = YES;
由于UIImageView默认禁用了用户交互,因此添加到图像视图的任何子视图都不会获得触摸事件。
希望它能解决您的问题,如果是,请投票。
此致 Satendra