Iphone 从其他控件继承的值

Iphone 从其他控件继承的值,iphone,ios,uitableview,inheritance,uipickerview,Iphone,Ios,Uitableview,Inheritance,Uipickerview,我是编程新手。我对继承有一个基本的怀疑。我在ViewController中有一个UITableView,当我选择Tableview的第一行时,我会进入Picker页面,在该页面中,我只需从UIPickerView中选择值。现在我必须选择NSLOG按钮中的视图控制器中的选择器的值。这是我面临的一个小问题 VIewController.m - (void)viewDidLoad { [super viewDidLoad]; tableview =[[UITableView alloc]init

我是编程新手。我对继承有一个基本的怀疑。我在
ViewController
中有一个
UITableView
,当我选择
Tableview
的第一行时,我会进入
Picker
页面,在该页面中,我只需从
UIPickerView
中选择值。现在我必须选择
NSLOG
按钮中的
视图控制器中的
选择器的值。这是我面临的一个小问题

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;
}
- (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"];



}
Picker.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;
}
- (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"];



}
您可以使用代理

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

@protocol pickerDelegate <NSObject>
将此协议的属性设置为

@property(nonatomic,weak)id<pickerDelegate>delegate
现在在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中声明的字符串变量,并在按钮单击事件上打印此字符串。听起来您需要一个委托,让选择器将所选值传递回表视图。在Picker.h文件顶部定义委托方法:

@class Picker;

@protocol PickerDelegate <NSObject>

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

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

pick.delegate = self;

您可以使用本地通知进行此操作。您能否解释更多…我做了什么…我制作了拾取程序的
字符串
非原子,保留了
,并且在视图中控制器id做了…
@property(非原子,保留)Picker1*delegate1然后
nslog(@“value%@”,delegate1.string)在按钮按下操作中…但当我单击按钮时没有显示任何内容…表示值为null@spider1983实际上,我必须选择这些值,然后通过在ViewController中获取这些值,必须执行一些操作..但无法获取值使用此行[yourPickerView selectRow:1不完整项:0动画:否]@Christien,你在那里创建了你的pickerview.还有一件事你必须让你的viewcontroller符合interfaceok中的pickerDelegate如果我需要在按钮中给出条件,比如如果pickers的值从picker==A,打开url…如何做…就像我在按钮{
if(piker1.string==@“raj”){[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@”http://www.google.com“]];}
if([picker1.string isEqualToString:@“raj”])我将其用于
[UIApplication sharedApplication]openURL:[NSURL URLWithString:@”http://www.google.com"]]
要检查…但它不是打开URL请尝试在webView中打开它,谢谢..如果我需要在Viewcontroller的按钮中给出条件以检查所选的第一个值并给出类似..
if(piker1.string=@“a”){[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@]http://www.google.com"]];}
您在ViewController.m中实现此委托方法,因此您有-(void)picker:(picker*)picker didPickValue:(NSString*)值{//在此处调用您的代码!}当picker调用委托方法时,它将在ViewController.m中实现。从那里,您可以对值执行任何您想要的操作我在视图控制器中所做的…@property(nonatomic,retain)Picker1*delegate1;…然后按下按钮->
nslog(@“value”,delegate1.string)
It's fine./…如果使用picker值给定条件,则给出正确的输出…
if([Picker1.string isEqualToString:@“raj”){[UIApplication sharedApplication]openURL:[NSURL URLWithString:@”http://www.google.com“]];}
检查未打开的值
@interface ViewController : UITableViewController <PickerDelegate>
[self.delegate picker:self didPickValue:[NSString stringWithFormat:@"%@",[list objectAtIndex:row]]];
pick.delegate = self;