2011-08-02 27 views
0

我正在寫我的第一個罪惡的iPhone應用程序。 我的登錄屏幕已經準備就緒,我在thinK。我在我的MainWindow中實現了一個用於登錄憑據的視圖,並將所有用戶界面添加到視圖(文本框等)。iOS:初學者幫助我的第一個應用程序:切換視圖?

現在呢?

我想檢查我的憑據,如果沒有問題,我會殺死主窗口的登錄視圖並顯示包含所有信息的主視圖。

怎麼辦?

我可以輕鬆地在loginviewcontroller中添加按鈕事件,檢查logindata那裏並獲得對主窗口的訪問權,並從視圖本身殺死沒有問題? 或者我必須在主窗口的視圖中聲明按鈕事件,並在那裏做所有事情?

我的代碼:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDelegate:(id)delegate 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = CGRectMake(self.view.frame.size.width/2 - 55.0f, self.view.frame.size.height/2 + 85.0f 
           , 110.0f, 35.0f); 
     [button setTitle:@"title" forState:UIControlStateNormal]; 
     [button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents: UIControlEventTouchDown]; 
     [self.view addSubview:button]; 
    } 
    return self; 
} 
+0

立即嘗試編輯的代碼@kovu並閱讀編輯註釋以查看您做錯了什麼。也請在下一次格式化你的代碼時,我想你知道你有1588個重複點。請接受答案和+1,如果它的工作:) –

回答

1
在您的視圖 - 控制

//maybe you will need to have more arguments added to you init method 
//for instance you might be using -(id)initWithNibName:bundle: right now 
//then just add delegate to it like -(id)initWithNibName:bundle:andDelegate: 
//just to clarify: delegate = reference to class you want to call a method in 
-(id)initMethod:(id)delegate { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    self.button.frame = CGRectMake(self.view.frame.size.width/2 - 55.0f, self.view.frame.size.height/2 + 85.0f 
            , 110.0f, 35.0f); 
    [button setTitle:@"title" forState:UIControlStateNormal]; 
    [button addTarget:delegate action:@selector(yourMethodInMainWindow:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 

} 

類與主窗口:

-(void)yourMethodInMainWindow:(id)sender { 
    //do whatever you want to do. 
} 

這就是設置它編程的方法之一。如果你使用的是筆尖,那麼你將在你的.h文件中定義一個類型爲- (IBAction)buttonClicked:(id)sender的方法,然後在界面構建器中連接它們。

我想你知道如何推新視圖。可以用例如presentModalViewController:animated:完成。所有這一切已經在stackoverflow和谷歌。

答案here可能告訴你你需要知道的關於訪問UIApplication以及窗口的信息。

+0

嗨, 你告訴我已經知道的事情。 問題是,如何從坐在視圖中的Button-Method調用MainWindow中的方法? – Kovu

+0

如何將mainWindow作爲'delegate'傳遞給視圖並像這樣使用它:'button addTarget:delegate action:@selector(whatEverMethodInMainWindow :) forControlEvents:UIControlEventTouchUpInside]; ' –

+0

我認爲它正是我正在尋找的,但我不明白如何使用此代碼。你能舉一個小例子/如何在你身上回答? – Kovu

0

您應該使用導航控制器的不同意見之間的轉換。

首先,在導航控制器插入導航控制器插入的MainWindow.xib

然後,負載LoginViewController。 LoginviewController將會放置你的登錄ID和密碼以及登錄btn的視圖。現在

,用於登錄BTN行動,寫一個方法,

- (IBAction) loginBtnPressed : (id) sender{ 

    MainViewController *newObject = [MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; 

    [self.navigationController pushViewController:newObject animated:YES]; 

} 

你寫此方法後,就將此方法附加到BTN在你的.xib文件中的界面生成器。