如何使用其他文件中的字符串?

时间:2011-12-11 09:56:23

标签: iphone xcode string file uitableview

我在一个文件中有一个变量字符串,但我需要在另一个文件中使用它。这是一个面包店应用程序,它需要接受订单。 MasterViewController中有一个桌面视图,面包店需要知道首先拍摄的是什么。有没有办法做到这一点?

这是我的代码:

MasterViewController

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"detailViewController"bundle:nil];
    detailViewController.item = [self.menu objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
    NSString *selectedItem = [menu objectAtIndex:indexPath.row];
}

OrderViewController

-(IBAction)orderButtonTapped:(id)sender {
    int whereString = [wherePicker selectedRowInComponent:1];
    NSString *mobileString = [mobileField text];
    int amountNumber = [amountPicker selectedRowInComponent:1];
    NSString *specialString = [specialField text];
    NSString *itemString = /*get the string from file here*/
    NSString *orderString = [[NSString alloc] initWithFormat:@"I would like %d %@ delivered to %@. My mobile number is %@. %@", amountNumber, itemString, whereString, mobileString, specialString];
}

1 个答案:

答案 0 :(得分:1)

OrderViewController中创建属性,然后将值分配给主视图控制器中的属性。我希望您知道如何创建属性,并且可以将值分配给事件中的属性...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"detailViewController"bundle:nil];
    detailViewController.item = [self.menu objectAtIndex:indexPath.row];
    NSString *selectedItem = [menu objectAtIndex:indexPath.row];

    // Here you need to assign the value to the property
    detailViewController.theItemString = selectedItem;
    [self.navigationController pushViewController:detailViewController animated:YES];
}