接上次传图不清晰问题
感觉运行不出来的原因就是传的这几个Label,(个人认为确定不了到底传的是哪个Label,那怎么办呢?)可以写下代码或具体解决方案吗?
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
NSArray *array_table;
NSArray *array1;
NSArray *array2;
NSArray *array3;
UIPickerView *pickerView;
NSString *tableNum;
NSString *mainFood;
NSString *fruitFood;
NSString *drinkFood;
}
@property (weak, nonatomic) IBOutlet UIButton *bt_submit;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
array_table=[[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
array1=[[NSArray alloc]initWithObjects:@"米饭",@"面条",@"窝窝", nil];
array2=[[NSArray alloc]initWithObjects:@"青椒肉丝",@"水晶牛柳",@"娃娃菜", nil];
array3=[[NSArray alloc]initWithObjects:@"可乐",@"绿茶",@"加多宝",@"开水", nil];
pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 414, 200)];
pickerView.delegate=self;
pickerView.dataSource=self;
[self.view addSubview:pickerView];
UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(20, 350, 60, 30)];
label1.text=@"主食";
[self.view addSubview:label1];
label1.textColor=[UIColor blackColor];
UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(20, 410, 60, 30)];
label2.text=@"主菜";
[self.view addSubview:label2];
label2.textColor=[UIColor blackColor];
UILabel *label3=[[UILabel alloc]initWithFrame:CGRectMake(20, 470, 60, 30)];
label3.text=@"饮料";
[self.view addSubview:label3];
label3.textColor=[UIColor blackColor];
UILabel *label4=[[UILabel alloc]initWithFrame:CGRectMake(120, 350, 60, 30)];
//label4.text=mainFood;
[self.view addSubview:label4];
UILabel *label5=[[UILabel alloc]initWithFrame:CGRectMake(120, 410, 60, 30)];
// label5.text=fruitFood;
[self.view addSubview:label5];
UILabel *label6=[[UILabel alloc]initWithFrame:CGRectMake(120, 410, 60, 30)];
// label6.text=drinkFood;
[self.view addSubview:label6];
UIButton *bt=[[UIButton alloc]initWithFrame:CGRectMake(70, 250, 60, 50)];
[bt setTitle:@"点菜" forState:UIControlStateNormal];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[bt addTarget:self action:@selector(buttonAction::::) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 4;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component==0){
return array_table.count;
}else if(component==1){
return array1.count;
}else if (component==2){
return array2.count;
}else if (component==3){
return array3.count;
}else{
return 0;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *str=nil;
if (component==0){
str=[array_table objectAtIndex:row];
}else if(component==1){
str=[array1 objectAtIndex:row];
}else if(component==2){
str=[array2 objectAtIndex:row];
}else if(component==3){
str=[array3 objectAtIndex:row];
}
return str;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component==0){
tableNum=[array_table objectAtIndex:row];
}else if (component==1){
mainFood=[array1 objectAtIndex:row];
}else if (component==2){
fruitFood=[array2 objectAtIndex:row];
}else if (component==3){
drinkFood=[array3 objectAtIndex:row];
}
}
-(void)buttonAction:(UIButton *)bt1 label:(UILabel *)label4 labe:(UILabel *)label5 label:(UILabel *)label6{
[bt1 setTitle:[NSString stringWithFormat:@"第%@桌",tableNum] forState:UIControlStateNormal];
label4.text=mainFood;
label5.text=fruitFood;
label6.text=drinkFood;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
正在回答
你这里buttonAction:方法里只是有参数,但是实际这些label并没有传递参数,不建议这么去实现。
把-(void)buttonAction:(UIButton *)bt1 label:(UILabel *)label4 labe:(UILabel *)label5 label:(UILabel *)label6这个方法的参数之保留-(void)buttonAction:(UIButton *)bt1这样,然后[bt addTarget:self action:@selector(buttonAction::::) forControlEvents:UIControlEventTouchUpInside];这里修改成[bt addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];再把定义的label1、label2、label3定义成全局属性就可以了。
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 516 人
- 提交作业 158 份
- 解答问题 637 个
本路径采用基础+案例方式,助你解开对界面优化、数据储存、屏幕适配的疑惑。6小时团购项目实战加最新版本Swift讲解,让你掌握更多iOS开发技巧。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星