2010-07-26 62 views
1

我有一個名爲PatternsViewController的視圖和一個名爲SolutionsViewController的子視圖。我想在PatternsViewController命名迭代變量傳遞給我的SolutionsViewController,之前,我與在視圖和它的子視圖之間傳遞一個int

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

回答

1

我想通了通過稍微修改Squeegy的代碼。

在PatternsViewController.m

[solutions setIteration:iteration]; 

和SolutionsViewController.m

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

謝謝哥們..:P :) – shivam 2013-05-16 08:37:20

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; 
} 
+0

這看起來很簡單,我喜歡它,如果它是這個容易,但我有一個我的方法在SolutionsViewController中出錯。編譯器期望在'{'之前有一個'('之前的'值'和一個標識符,我對此很陌生,我不知道該把'('。 – WFT 2010-07-27 02:21:45

+0

我想出來了,只有一個對你給我的代碼進行了一些小的調整,非常感謝 – WFT 2010-07-27 20:17:37

+0

哈,對不起,我最近一直在寫Java,並且讓我的語法困惑...... – 2010-07-27 21:51:24

0

我會用一個Singleton類出示。

What should my Objective-C singleton look like?

然後,你可以這樣做:

[SingletonClass sharedInstance].var = iteration; 

並訪問它:

[SingletonClass sharedInstance].var 
+0

所有那些爲單個變量設置Singleton的工作......可能需要看看@擠壓的答案,也是你提出的最後一個問題http://stackoverflow.com/questions/3325159/how-to-pass-text-between-views/3325992#3325992。只是說這是因爲那裏是一種更簡單的方式 – iwasrobbed 2010-07-26 23:30:53

+0

是的,這很簡單,我從來沒有真的想過這樣做。 – Kurbz 2010-07-26 23:37:11

0

爲什麼就不能過度乘坐的init與採取詮釋你想設置一個額外的參數?這允許在沒有添加set調用的情況下進行乾淨的實例化。

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

所選答案適用於我,但它給了我一個語義警告。即使代碼正常工作,我仍然對警告保持警惕,所以我想知道是否有辦法在沒有警告的情況下使其工作。

的警告是:

實例方法「-SetPrompt:」未找到(返回類型默認爲「身份證」)

這裏是在沿在這個問題的答案下面我做了什麼。

在調用.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