[英]Using global variables in objective c
如何在objective-c
中将数据从一个视图控制器传递到另一个视图控制器? 我swift
就知道会在类声明之外创建一个全局变量,但是在objective-c
中,有什么方法可以将数据从选定的单元格传递到用户选择该单元格时显示的新视图?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BarInfoCell"];
[cell textLabel].text = [[bars objectAtIndex:indexPath.row] objectForKey:@"name"];
NSString *barAddress = [NSString stringWithFormat: @"%@ %@ %@", [[bars objectAtIndex:indexPath.row] objectForKey:@"address"],
[[bars objectAtIndex:indexPath.row] objectForKey:@"state"],
[[bars objectAtIndex:indexPath.row] objectForKey:@"zip"]];
[cell detailTextLabel].text = barAddress;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"detailSeg" sender:self];
}
考虑您要从ViewController1将字符串数据发送到ViewController2,ViewController3。
在ViewController2和ViewController3中,使字符串变量具有属性。
@property(nonatomic,strong) NSString *str;
在推送ViewController2和ViewController3的同时:
ViewController2 *viewController = [ViewController2 alloc]init];
viewController2.str = @"Some text";
[self.navigationController pushViewController:viewController2 animated:YES];
并且您已从ViewController2中的ViewController1发送数据。
您需要像这样稍微更改一下didSelectRowAtIndexPath
,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"detailSeg" sender:indexPath];
}
如果在prepareForSegue
方法中使用UINavigationController
,则可以将数据从一个视图发送到另一个视图,例如,
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"detailSeg"]) {
NextViewContorller nVC = (NextViewContorller)segue.destinationViewController;
NSIndexPath *ip = (NSIndexPath *)sender;
//You need to define variable in which you want to pass data, in this case value1,value2
//WAY 1
NSString *barAddress = [NSString stringWithFormat: @"%@ %@ %@", [[bars objectAtIndex:ip.row] objectForKey:@"address"],
[[bars objectAtIndex:ip.row] objectForKey:@"state"],
[[bars objectAtIndex:ip.row] objectForKey:@"zip"]];
nVC.value1 = [[bars objectAtIndex:ip.row] objectForKey:@"name"]
nVC.value2 = barAddress;
//OR
//WAY 2
UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:ip];
nVC.value1 = cell.textLabel.text;
nVC.value2 = cell.detailTextLabel.text;
}
}
这是提供帮助的数据从一个UIViewController
传递到另一个UIViewController
。
步骤1:在第二个UIViewController
(.h文件)中将属性声明为Objective-C对象。 该对象可能是像NSDictionary
, NSArray
等之类的东西。在这里,我为NSString
创建了属性。
@property (nonatomic, retain) NSString *strTitle;
步骤2:从一个UIViewController
转到另一个UIViewController
时,您拥有另一个UIViewController
的对象。 一旦有了对象,并且如果您像第1步那样创建了property
则可以使用类对象访问创建的属性,如下所示。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"detailSeg"]) {
DetailViewController * objDetails = (DetailViewController * )segue.destinationViewController;
objDetails.strTitle = "Hello";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.