【问题标题】:Pass an int between a view and it's subview在视图和它的子视图之间传递一个 int
【发布时间】:2010-07-26 23:11:52
【问题描述】:

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

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

【问题讨论】:

    标签: iphone objective-c cocoa-touch modalviewcontroller


    【解决方案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;
    }
    

    【讨论】:

    • 这看起来很简单,如果它这么简单,我会喜欢的,但是我在 SolutionsViewController 中的方法有错误。编译器期望在“值”之前有一个“(”,在“{”之前有一个标识符。我对此有点陌生,我不知道将“(”放在哪里。
    • 我想通了,只是对你给我的代码做了一些小的调整。非常感谢
    【解决方案2】:

    我通过稍微修改 Squeegy 的代码就知道了。

    在 PatternsViewController.m 中

    [solutions setIteration:iteration];
    

    在 SolutionsViewController.m 中

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

    【讨论】:

      【解决方案3】:

      我会使用单例类。

      What should my Objective-C singleton look like?

      然后你可以这样做:

      [SingletonClass sharedInstance].var = iteration;
      

      并通过以下方式访问它:

      [SingletonClass sharedInstance].var
      

      【讨论】:

      • 为单个变量设置单例的所有工作...可能想看看@squeegy 的答案以及您为stackoverflow.com/questions/3325159/… 提出的最后一个问题。之所以这么说,是因为有更简单的方法。
      • 是的,这要简单得多,我从来没有真正想过这样做。
      【解决方案4】:

      为什么不简单地用一个额外的参数来覆盖 init 呢?这允许在不添加 set 调用的情况下进行干净的实例化。

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

      【讨论】:

        【解决方案5】:

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

        警告是:

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

        【讨论】:

          猜你喜欢
          • 2014-06-02
          • 1970-01-01
          • 2013-09-15
          • 2014-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多