2016-06-29 7 views
0

私はUIAlertControllerを持っています。このオプションでは配列からデータが取り込まれ、ユーザーに提示されます。ユーザーは、アラートからオプションを選択します。この後、別の警告が表示され、ユーザーに確認ボタンがある確認メッセージが表示されます。2つのUIAlertControllersを連続して提示しようとしています

myAlert.addAction(UIAlertAction.init(title: item, style: .Default, handler: { 
    (UIAlertAction) in 
     self.chosenBusiness.append(businessNameData[item]!) 
})) 
self.presentViewController(myAlert, animated: true, completion: nil) 

上記のコードは、配列からデータを収集し、myAlertのアクションにプッシュします。上のコードはforループの内部です。

この後、私は一番上のビューコントローラを取得する関数を使い、次の警告を押します。

let top = topMostController() 
let alertController = UIAlertController(title: "Location pinned", message: "You've successfully pinned this location, good work!", preferredStyle: UIAlertControllerStyle.Alert) 
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
    (result : UIAlertAction) -> Void in 
    print("OK") 
} 
alertController.addAction(okAction) 

self.presentViewController(myAlert, animated: true, completion: nil) 
top.presentViewController(alertController, animated: true, completion: { 
    _ in 
}) 

Iは受信エラーがある:それは 割り当て解除され、許可されておらず、未定義の動作を引き起こすかもしれないビューコントローラのビューをロードしようと

。 UIAlertController:0x1535b1cd0。

誰かがこれを手伝ってくれますか?

+1

なぜ2番目のコードブロックで 'self.presentViewController(myAlert、animated:true、completion:nil)'を呼び出すのですか?最初のコードブロックに最初のアラートを表示していませんか?また、第1のアラートの完了ブロックを使用して、第2のアラートを表示することができますか(条件によっては、ユーザー操作に基づいています)。 – wottle

+0

元のAlertControllerのアクションハンドラ内で、確認AlertControllerのプレゼンテーションを呼び出すことができます。一番上のコントローラで実行する必要はありません。元のものと同じように、自分で実行してください。さらに、アクションクロージャにはサイクルが保持されます。閉じた状態での暗黙の強い保持を防ぐためには、[無所属の自己]または[弱い自己]として自分自身を捉える必要があります。 – SArnab

+0

私はmyAlertのself.presentViewControllerを移動しましたが、影響はありません。 SArnab、あなたはより多くのことを詳しく説明できますか? – vkrishnan23

答えて

2

私はこれがあなたが探しているものだと思います。第二のものは、第一のものの解雇行為で呼び出さなければならない。また、UIを使用して作業するときはいつでも、あなたが現在メインキューにいるという肯定的でない場合よりも安全です。dispatch_async(dispatch_get_main_queue()) { \\code }

let firstAlertController = UIAlertController(title: "First", message: "This is the first message.", preferredStyle: UIAlertControllerStyle.Alert) 

let secondAlertController = UIAlertController(title: "Second", message: "This is the second message.", preferredStyle: UIAlertControllerStyle.Alert) 
let secondDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, completion: nil) 
secondAlertController.addAction(secondDismissAction) 

let firstDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default) { 
    UIAlertAction in 
    dispatch_async(dispatch_get_main_queue()) { 
     self.presentViewController(secondAlertController, animated: true, handler: nil) 
    } 
} 

firstAlertController.addAction(firstDismissAction) 
self.presentViewController(firstAlertController, animated: true, completion: nil) 
+0

Sethmr、私は配列からそれらをプッシュしているので、myAlertのアラートアクションを設定するためにdispatch_asyncを使用しました。しかし、私は両方のalertcontrollersのdispatch_asyncを使用することによって意味が失われている – vkrishnan23

+0

私はちょうど私が書いた関数をテストしました。私のテキストを自分のテキストに置き換えるだけで、それが実行されます。 presentingViewController行にはdispatch_asyncのみを使用し、メインキューにない行にはdispatch_asyncを使用します。私の目的では、補完ハンドラクロージャを使って非同期関数を呼び出さない限り、メインキュー上で実行されるviewDidLoadに関数を配置したので、firstDismissAction内に配置しただけです。 – Sethmr

+0

第2のalertControllerは、第1の警告コントローラの解除アクションのように呼び出さなければなりません。 – Sethmr

関連する問題