[英]Using global variables in objective c
How can I pass data from one view controller to another in objective-c
? 如何在
objective-c
中将数据从一个视图控制器传递到另一个视图控制器? I know in swift
I would have just created a global variable outside the class declaration, but in objective-c
is there any way to pass data from the selected cell to a new view that is shown when the user selects that cell? 我
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];
}
Consider you want to send string data to ViewController2,ViewController3 from ViewController1. 考虑您要从ViewController1将字符串数据发送到ViewController2,ViewController3。
Make property of the string variable in ViewController2 and ViewController3. 在ViewController2和ViewController3中,使字符串变量具有属性。
@property(nonatomic,strong) NSString *str;
And while pushing the ViewController2 and ViewController3: 在推送ViewController2和ViewController3的同时:
ViewController2 *viewController = [ViewController2 alloc]init];
viewController2.str = @"Some text";
[self.navigationController pushViewController:viewController2 animated:YES];
And you have the data send from ViewController1 in ViewController2. 并且您已从ViewController2中的ViewController1发送数据。
You need to change your didSelectRowAtIndexPath
little like this, 您需要像这样稍微更改一下
didSelectRowAtIndexPath
,
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier:@"detailSeg" sender:indexPath];
}
You can send data from one view to another view if there is segue with UINavigationController
in prepareForSegue
methos like, 如果在
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;
}
}
Here is steps that provide help to pass data from one UIViewController
to another UIViewController
. 这是提供帮助的数据从一个
UIViewController
传递到另一个UIViewController
。
Step 1 : Declare property to objective-c object in second UIViewController
(.h file). 步骤1:在第二个
UIViewController
(.h文件)中将属性声明为Objective-C对象。 The object may be whatever like NSDictionary
, NSArray
etc. here i have created property for NSString
. 该对象可能是像
NSDictionary
, NSArray
等之类的东西。在这里,我为NSString
创建了属性。
@property (nonatomic, retain) NSString *strTitle;
Step 2: You have the object of another UIViewController
when you are going from one UIViewController
to another UIViewController
. 步骤2:从一个
UIViewController
转到另一个UIViewController
时,您拥有另一个UIViewController
的对象。 once you have object and if you created property
like step 1 then you can access the created property with class object like as below. 一旦有了对象,并且如果您像第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.