Search code examples
iosswiftuiviewuialertcontroller

How to display AlertController in UiView


new to Swift. Below code is executed without error but in the console it shows a message. Is it an error message and how to fix it? Is it required to be fixed when sending app for review.

Thanks

This is my App structure :

VC(1)     -->  goto -->    VC(2)

In VC(1) I have:

  • BtnGo
  • UiLabelMsg
  • UitableView

I created a Link to VC(2) : Control-Drag BtnGo to VC(2)

I named the segue : SegueToVC2

What I wanted to do:

I need to pass a string to VC2. Before passing check if user made a selection.

in PrepareForSegue,

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

    var strchk: String = UIlabelMsg.text!

    if strchk.isEmpty {

        var alert = UIAlertController(title: "Alert",message:" No Selection made",
           preferredStyle: UIAlertControllerStyle.Alert)

        alert.AddAction(UIAlertction(title: "OK",style:UIAlertActionStyle.Default,hanlder: nil))

        self.presentViewController(alert, animated: true, completion: nil ) 

    } else {

    if (segue.Identifier =="SegueToVC2") {

        var targetVC = segue.destinationViewControl as! VC2
        targetVC.strMsg = UILabelMsg.text
    }
}

Problem:

in the console, it shows this:

UIView : 0x7fdfc25668c0; frame =(0,0,375,667); autoresize = w+h; layer = 's windows is not equal to 's View's Window!

I have these questions

How come I don't need to write code for BtnGo when I use prepareForSegue? if I have more than 2 button? these 2 will handle by Segue?


Solution

  • UIView have not presentViewController method, what's the self? You can using window of UIView to presenting.

    let alert = UIAlertController(title: "Alert", message: "No Selection made", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
    

    EDIT: Possible you need to override the shouldPerformSegueWithIdentifier to check the UIlabelMsg.text.

    override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
        var strchk: String = UIlabelMsg.text!
        if strchk.isEmpty {
            var alert = UIAlertController(title: "Alert",message:" No Selection made",
            preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK",style:UIAlertActionStyle.Default,handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
    
            return false
        } else {
            return true
        }
    }