2016-05-20 7 views
-2

私は(attendanceViewController)からsecondViewControllerを提示し、パラメータを渡して関数を呼び出そうとしています。 AttendanceViewControllerが表示され、関数が呼び出されます。問題は(@IBOutlet weak var tableView: UITableView! , @IBOutlet weak var boxTypeSKU: UIView!....all)DismissViewController渡しパラメータback swift

self.presentingViewController!.dismissViewControllerAnimated(true, completion: { _ i 
      let attView: AttendanceViewController = self.storyboard!.instantiateViewControllerWithIdentifier("AttendanceViewID") as! AttendanceViewController 
       attView.currAttendance = self.currAttendance 
       attView.searchProductWithSKU("\(sku)") 

      }) 
+0

' AttendanceViewController'がインスタンス化されるかもしれませんが、ビューのライフサイクルプロセスのために、それらのアウトレットはおそらくまだ接続されません。 '-viewDidLoad'が' AttendanceViewController'で呼び出されるまで、 'searchProductWithSKU'を呼び出すことを延期する必要があるかもしれません – Aaron

答えて

0

を閉じたときに、すべてのオブジェクトがnilであると気づいたことがまず最初にAttendanceViewControllerの新しいインスタンスがインスタンス化されていることです。これは、プロパティが正しいオブジェクトに設定されていないことを意味します。 secondViewControllerを提示したビューコントローラへの参照が必要です。それがどのように行われるかはあなた次第ですが、currAttendance変数を含むコールバックをお勧めします。これは、提示されたView Controller上のプロパティです。提示されたビューコントローラによってコールバックが呼び出されると、親のAttendanceViewControllerはそれ自身のプロパティを設定し、提示されたビューコントローラを却下してsearchProductWithSKU(_:)メソッドを呼び出すことができます。

0

私はこのチュートリアル(http://swiftdeveloperblog.com/pass-information-back-to-the-previous-view-controller/)のようなプロトコルを使用して私の問題を解決しました。私はそれがよりエレガントで効率的だと思います。

私の更新されたコードがあります:

が、私はそれを行う第2のビューコントローラ(BarcodeScannerViewController.swift)の場合:最初のビューコントローラ(AttendanceViewController.swift)で

protocol BarcodeScannerProtocol { 
    func setSKUScanner(sku: String) 
} 


class BarcodeScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate { 
var delegate:BarcodeScannerProtocol? 

func back() { 
let sku = (barcode as NSString).substringWithRange(NSMakeRange(6, 8)) 
delegate?.setSKUScanner(sku) 
self.presentingViewController!.dismissViewControllerAnimated(true, completion: { _ in 
} 

} 

class AttendanceViewController: UIViewController, BarcodeScannerProtocol { 
    var strSKUScanner : String? 

    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 
     if let skuScanned = strSKUScanner { 
      searchProductWithSKU(skuScanned) 
     } else { 
      fetchProducts() 
     } 
    } 

// MARK: BarcodeScannerProtocol functions 
    func setSKUScanner(sku: String) { 
     self.strSKUScanner = sku 
    } 


} 
関連する問題