簡體   English   中英

在Xcode中按下按鈕時如何去相同的視圖控制器?

[英]how to go same view controller when button pressed in Xcode?

我正在開發一個登錄應用程序,並且我有一些疑問。 如果按下登錄按鈕,則該文本字段中的任何文本都應在下一個視圖控制器中顯示-如用戶名和密碼。 因此,我使用segues將值帶到下一個View Controller。

如果兩個文本字段的值都為null,然后按“登錄”按鈕,則顯示警報以填充兩個文本字段。 在警報中按OK按鈕時,程序將自動轉到第二個控制器。 當按下按鈕時,我不知道如何去使用相同的視圖控制器。 請幫助我。

我的代碼:

   -(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender
{ 
    second *transferViewController = [segue destinationViewController];
    if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ]) 
    { 
      UIAlertView * mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" message:@"This is the Iphone app" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
      [mes show]; 
    } 
} 

有很多不同的方法可以做到這一點。 更好的方法之一是使用未附加到動作的segue在情節提要中連接兩個場景。 之后,您可以以編程方式觸發視圖控制器中的segue。 您可以通過控制方式將故事板場景底部的文件所有者圖標拖動到目標場景。

將出現一個彈出窗口,要求在“ Manual Segue”中進行選擇; 選擇“推送”作為類型。 點擊小方塊,確保您位於“屬性”檢查器中。 給它一個標識符,您將在代碼中使用它來引用它。 接下來,您將使用一個按鈕觸發segue(或者您可以使用AlertView委托)。其選擇器如“ buttonAction”。 如果按下按鈕,將調用此功能。此功能可觸發搜索並轉到目標場景。 因此,該按鈕並在該方法內使用void()方法,並按如下方式調用segue:

-(void)buttonAction:(id)sender{
 if(textfield.text.length!=0){
    [self performSegueWithIdentifier:@"MySegueName" sender:sender];
  }
 else{
     alert!!
   }
}

無需prepareForSegue

 -(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender

  { second *transferViewController = [segue destinationViewController]; 

    }

將函數更改為IBAction方法

  -(IBAction)login:(id)sender
  {

     if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ]) 
     { 
       UIAlertView *mes=[[UIAlertView alloc] initWithTitle:@"Empty Fields !!!" message:@"enter the user details" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [mes show];

    }

   else

    {

        // both textfield are filled and validated
          [self performSegueWithIdentifier:@"your segue name of secondVC" sender:sender]; 
        }
     }

嘗試這個....

- (IBAction)loginButtonPressed:(UIButton *)sender
{
    if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ])
    {
        UIAlertView * mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" message:@"This is the Iphone app" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [mes show];
    }
    else
    {
        [self performSegueWithIdentifier:@"yoursegueID" sender:nil]; // 
    }

}



- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{

    if (buttonIndex == 0)
    {
        [self performSegueWithIdentifier:@"yoursegueID" sender:nil];
    }
}

還設置UIAlertViewDelegate

@interface YourViewController : UIViewController <UIAlertViewDelegate> //in which you used `UIAlertView`

我建議實現方法shouldPerformSegueWithIdentifier:此方法返回一個BOOL,指示是否應該繼續進行搜索-

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{ 

  BOOL ret=YES:

  if ([txt1.text isEqualToString:@"" || txt2.text isEqualToString:@"" ]) 
  { 
    UIAlertView * mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!" message:@"This is the Iphone app" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [mes show]; 
    ret=NO;
  } 

  return ret;
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM