1

我有一个名为 PatternsViewController 的视图和一个名为 SolutionsViewController 的子视图。我想将 PatternsViewController 中名为迭代的变量传递给我的 SolutionsViewController,就在我展示它之前

solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
 [self presentModalViewController:solutions animated:YES];
4

5 回答 5

1
solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

// Set your value here
[solutions setMyIntWithSomeMethodIWrote:123];

[self presentModalViewController:solutions animated:YES];

而在SolutionsViewController

- (void)setMyIntWithSomeMethodIWrote:(int)value {
    myInstanceVar = value;
}
于 2010-07-26T23:14:44.080 回答
1

我通过稍微修改 Squeegy 的代码来解决这个问题。

在 PatternsViewController.m 中

[solutions setIteration:iteration];

在 SolutionsViewController.m

-(void) setIteration:(int)value {
    iteration = value;
    NSLog(@"Iteration set from value: %d" , iteration);
}
于 2010-07-27T20:24:52.343 回答
0

我会使用单例类。

我的 Objective-C 单例应该是什么样的?

然后你可以这样做:

[SingletonClass sharedInstance].var = iteration;

并通过以下方式访问它:

[SingletonClass sharedInstance].var
于 2010-07-26T23:15:23.737 回答
0

为什么不简单地用一个额外的参数覆盖 init ,该参数采用你想要设置的 int ?这允许干净的实例化而无需添加集合调用。

solutions = [[SolutionsViewController alloc] initWithIntVal: int];
于 2010-07-27T20:37:37.160 回答
0

选定的答案对我有用,但它给了我一个语义警告。即使代码有效,我也对警告有点保留,所以我想知道是否有办法让它在没有警告的情况下工作。

警告是:

找不到实例方法“-SetPrompt:”(返回类型默认为“id”)

这是我在遵循这个问题的答案时所做的。

在调用 .m 文件中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    vcSelection *ViewSelection = [[vcSelection alloc] initWithNibName:@"vcSelection" bundle:nil];
    ViewSelection.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceType])
    {
        [ViewSelection SetPrompt:@"Select the device type."];
    }
    else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceManufacturer])
    {
        [ViewSelection SetPrompt:@"Select the device manufacturer."];
    }
    else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceModel])
    {
        [ViewSelection SetPrompt:@"Select the device model."];
    }
    [self.view addSubview:ViewSelection.view];
}

在接收 .m 文件中

@implementation vcSelection

NSMutableString *Prompt;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Debug"
                                                    message:Prompt
                                                   delegate:self
                                          cancelButtonTitle:@"Done"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void) SetPrompt:(NSMutableString *)Value
{
    Prompt = Value;
}

@end
于 2013-04-14T19:38:43.587 回答