2015-02-03 5 views
1

두 개의 텍스트 필드와 버튼이있는보기가 있습니다.스위프트 :보기간에 여러 값 전달

@IBOutlet var inputURL: UITextField! 
@IBOutlet var inputName: UITextField! 

@IBAction func submitUrlButton(sender: AnyObject) { 
} 

두 개의 변수가 두 번째보기 : 스위프트에서

var submittedURL = "" 
var submittedName = "" 

println("Name \(submittedName)") 
println("URL \(submittedURL)") 

어떻게 두 개의 텍스트 필드에 입력 된 값을 전달하고 두 번째보기에서 해당 변수에 할당합니까?

감사 THETOM에 대한

편집 :

import UIKit 

class ViewController: UIViewController { 

@IBOutlet var inputURL: UITextField! 

@IBAction func submitBtn(sender: AnyObject) { 
    performSegueWithIdentifier("submissionSegue", sender: self) 
} 


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 

    // Create a new variable to store the instance of the next view controller 
    let destinationVC = segue.destinationViewController as BrandsViewController 
    destinationVC.submittedURL.text = inputURL.text 
} 
} 
+0

(3210) 당신은 다음과 같은 사항에 대해 자세히 설명 할 수 있습니까? - 뷰 또는 뷰 컨트롤러를 누가 생성합니까? - 두보기간에 링크가 있습니까? –

+0

@ TheTom 예 그들은 viewcontrollers이고 그들은 'submissionSegue'라는 segue 있습니다. – DannieCoderBoi

답변

4

당신은 방법 prepareForSegue를 사용할 수 있습니다.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 

    // Create a new variable to store the instance of the next view controller 
    let destinationVC = segue.destinationViewController as CustomViewController 
    destinationVC.submittedURL = inputURL.text 
    destinationVC.submittedName = inputName.text 
} 
다음

CustomViewController은 SEGUE가 예정되어있는 UIViewController에의 사용자 정의 클래스입니다 : 첫 번째보기합니다 (SEGUE에서 오는되는 것)에서

다음과 같은 코드를 작성합니다.

은 버튼 @IBAction에 프로그래밍 SEGUE을 수행하려면 않는 :

@IBAction func buttonWasClicked(sender: AnyObject) { 
    performSegueWithIdentifier("submissionSegue", sender: self) 
} 
+0

그런 다음 어떻게 버튼을 사용하여 연속 변경을 시작합니까? – DannieCoderBoi

+0

또한 viewcontroller 코드를 추가 할 때 : 선언되지 않은 형식 'UIViewController' – DannieCoderBoi

+0

사용 segue IBAction : performSegueWithIdentifier ("submissionSegue", 보낸 사람 : self)이 추가하십시오 –

1

뷰 컨트롤러가 SEGUE와 연결되어 있기 때문에 먼저 뷰 컨트롤러에서 prepareForSegue 방법을 무시하고 너무

을 수행하여 데이터를 전달할 수 있습니다
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if (segue.identifier == "secondViewController") { // here secondViewController is your segue identifier    
     var secondViewController = segue.destinationViewController as SecondViewController // where SecondViewController is the name of your second view controller class 
     secondViewController.submittedURL = inputURL.text 
     secondViewController.submittedName = inputName.text 
    } 
} 

그리고 performSegue로 버튼 액션 내부에서 perfromSegueWithIdentifier 방법

@IBAction func submitUrlButton(sender: AnyObject) { 

    //replace identifier with your identifier from storyboard 
    self.performSegueWithIdentifier("secondViewController", sender: self) 
} 
- 그 뷰 또는 뷰 컨트롤러는 :
0
The simplest way of accessing values globally not neccessary to pass with segue 

First View controller

import UIKit 
var submittedURL:NSString? // declare them here 
var submittedName:NSString? // Now these two variables are accessible globally 
class YourViewController : UIViewController 
{ 
@IBOutlet var inputURL: UITextField! 
@IBOutlet var inputName: UITextField! 

@IBAction func submitUrlButton(sender: AnyObject) { 
if inputURL.text == "" && inputName.text == "" 
{ 
//Show an alert here etc 
} 
else { 
self.submittedURL.text = inputURL.text 
self.submittedName.text = inputName.text 
} 
} 
} 

SecondView Controller

import UIKit 
class SecondviewController: UIViewController 
{ 
//inside viewDidload 
println(submittedURL) 
println(submittedName) 
}