1

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

视图控制器.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;
}

选择器.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"];



}
4

2 回答 2

1

您可以使用委托

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

@protocol pickerDelegate <NSObject>

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

@end

使这个协议的财产

@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 中实现 dlegate 方法

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

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

于 2012-12-14T11:43:39.493 回答
1

听起来您需要一个委托来让您的选择器将选定的值传递回表格视图。在 Picker.h 文件的顶部定义委托方法:

@class Picker;

@protocol PickerDelegate <NSObject>

- (void)picker:(Picker *)thePicker didPickValue:(NSString *)theValue;

@end

同样在您的 Picker.h 文件中,创建与代理的弱连接,代理将是表视图控制器:

@property (nonatomic, weak) id <PickerDelegate> delegate;

在您的表视图控制器中,将委托添加到头文件中:

@interface ViewController : UITableViewController <PickerDelegate>

您从 Picker.m 调用委托方法并在 ViewController.m 中实现它:

[self.delegate picker:self didPickValue:[NSString stringWithFormat:@"%@",[list objectAtIndex:row]]];

重要提示:当您创建选择器时,您必须告诉它代表将是谁。您正在表格视图中执行此操作,因此您调用:

pick.delegate = self;
于 2012-12-14T11:52:35.800 回答