gpt4 book ai didi

iphone - 从其他控件继承的值

转载 作者:行者123 更新时间:2023-11-29 11:05:23 24 4
gpt4 key购买 nike

我是编程新手。我对继承有一个基本的怀疑。我有一个 UITableViewViewController当我选择 Tableview 的第一行时.它带我到Picker只有我必须从 UIPickerView 中选择值的页面很好,现在我必须 NSLOG选择值 pickerViewControllerbutton .这是我面临的小问题。

VIewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

tableview =[[UITableView alloc]initWithFrame:CGRectMake(0,0,([UIScreen mainScreen].bounds.size.width),([UIScreen mainScreen].bounds.size.height/2)) style:UITableViewStyleGrouped];
tableview.delegate=self;
tableview.dataSource=self;
[self.view addSubview:tableview];




button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"START " forState:UIControlStateNormal];
button.frame = CGRectMake(62, 250, 196, 37);
[self.view addSubview:button];



thearray= [[NSArray alloc]initWithObjects:@"A",@"B ",@"C",@"D",@"E" ,nil];



[super viewDidUnload];
}

-(void)pressed{
// nslog value..i need here the picker value
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


return 5;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


if(indexPath.row==0){

Picker1 *pick= [[Picker1 alloc] init];


[self navigationController] pushViewController:pick animated:YES]
}


}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS IndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}


cell.textLabel.text=[thearray objectAtIndex:indexPath.row];

return cell;
}

Picker.m

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

return [list count];

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

return [list objectAtIndex:row];

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

// update label text to show selected option

string=[NSString stringWithFormat:@"%@",[list objectAtIndex:row]];

label.text=string;


[self dismissModalViewControllerAnimated:YES];



}


- (void)viewDidLoad
{
[super viewDidLoad];

list =[[NSMutableArray alloc]init];
[list addObject:@"A"];
[list addObject:@"B"];
[list addObject:@"C"];



}

最佳答案

你可以使用委托(delegate)

在 picker.m 中,在 @interface 部分上方执行此操作

@protocol pickerDelegate <NSObject>

-(void)didFinishPicking:(NSString *)pickedStr;

@end

使该协议(protocol)具有属性

@property(nonatomic,weak)id<pickerDelegate>delegate

在 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 方法中,执行此操作

string=[NSString stringWithFormat:@"%@",[list objectAtIndex:row]];



[self.delegate didFinsihPicking:string];

现在在 viewcontroller.h 中,在方法中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


Picker1 *pick= [[Picker1 alloc] init];

[pick setDelegate : self];

并在viewcontroler.h中实现delgate方法

    -(void)didFinishPicking:(NSString *)pickedStr
{
[self setStr:pickedStr]
}

其中 str 是您需要在 viewcontroller.h 中声明并在按钮单击事件上打印此字符串的字符串变量

关于iphone - 从其他控件继承的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13877682/

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