[英]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.