在视图与其子视图之间传递 int

发布于 2024-09-11 04:26:04 字数 356 浏览 7 评论 0原文

我有一个名为 PatternsViewController 的视图和一个名为 SolutionsViewController 的子视图。我想在将 PatternsViewController 中名为迭代的变量传递给我的 SolutionsViewController 之前,先将其呈现

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

I have a view called PatternsViewController and a subview named SolutionsViewController. I want to pass a variable in PatternsViewController named iteration to my SolutionsViewController, right before I present it with

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的軌跡 2024-09-18 04:26:05

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

在 PatternsViewController.m

[solutions setIteration:iteration];

和 SolutionsViewController.m 中

-(void) setIteration:(int)value {
    iteration = value;
    NSLog(@"Iteration set from value: %d" , iteration);
}

I figured it out by slightly modifying Squeegy's code.

In PatternsViewController.m

[solutions setIteration:iteration];

and in SolutionsViewController.m

-(void) setIteration:(int)value {
    iteration = value;
    NSLog(@"Iteration set from value: %d" , iteration);
}
囚我心虐我身 2024-09-18 04:26:05

我会使用 Singleton 类。

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

然后你可以这样做:

[SingletonClass sharedInstance].var = iteration;

并通过以下方式访问它:

[SingletonClass sharedInstance].var

I would use a Singleton class.

What should my Objective-C singleton look like?

Then you can do like:

[SingletonClass sharedInstance].var = iteration;

And access it with:

[SingletonClass sharedInstance].var
多彩岁月 2024-09-18 04:26:05

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

solutions = [[SolutionsViewController alloc] initWithIntVal: int];

Why not simply over-ride the init with an additional argument that take the int you want to set? This allows a clean instantiation without an added set call.

solutions = [[SolutionsViewController alloc] initWithIntVal: int];
橘香 2024-09-18 04:26:05

所选答案对我有用,但它给了我一个语义警告。即使代码有效,我对警告还是有点记忆犹新,所以我想知道是否有一种方法可以让它在没有警告的情况下工作。

警告是:

找不到实例方法“-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

The selected answer works for me but it is giving me a semantic warning. I'm a little annal retentive about warnings even if the code works so I am wondering if there is a way to make it work without the warning.

The warning is:

Instance method '-SetPrompt:' not found (return type defaults to 'id')

Here is what I did while following along to the answer in this question.

In the calling .m file

- (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];
}

In the receiving .m file

@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
简单气质女生网名 2024-09-18 04:26:04
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;
}
solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

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

[self presentModalViewController:solutions animated:YES];

And in SolutionsViewController

- (void)setMyIntWithSomeMethodIWrote:(int)value {
    myInstanceVar = value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文