Jack
Jack

Reputation: 81

UIAlertController Giving error? Please Give the code for UIAlertController with Array of Strings As Buttons in Swift

This is my code: Please Check whether anything went Wrong? Please give me the code for UIActionsheet (UIAlertController) with array in swift

let alertA = UIAlertController(title: "PlayLists", message: "Select From PlayLists Below", preferredStyle: .ActionSheet)

let action = UIAlertAction(title: "Cancel", style:  .Default, handler: {(alert: UIAlertAction!) in

})

let playListModel = PlayListModel.sharedInstance
plLists = playListModel.getPlayListNames()

for(var i = 0; i < plLists.count; i++){

    alertA.addAction(UIAlertAction(title: plLists.objectAtIndex(i) as? String, style: .Default, handler: {
    action in

        NSLog("%@",String(i))

    }))
}

alertA.addAction(action)
self.presentViewController(alertA, animated: true, completion: nil)

In this code if I have 3 playlists the actionsheet shows three playlists along with cancel button. When I click the first button it will print value 4(where i stopped incrementing). Why is it printing this? It should print i value with 1, right? What's the problem? Is the Action inside the For Loop is Correct?

Upvotes: 0

Views: 356

Answers (2)

Victor Sigler
Victor Sigler

Reputation: 23451

First of all I going to explain you why this happen, you can see if you touch any of your buttons always the output result of the call of NSLog("%@",String(i)) is plLists.count - 1, this is because you're trying to set always the index of the for statement inside the closure of the actions you're adding. The variable is outside the scope of the closure.

The main problem is that always the reference retained inside the closure in your case is the last index of the array, because as you said is increased and finally when it finish is assigned the value.

Nevertheless, you can take an approach for the title property in the action, like in the following way:

func presentAction() {

    let alertA = UIAlertController(title: "PlayLists", message: "Select From PlayLists Below", preferredStyle: .ActionSheet)
    let action = UIAlertAction(title: "Cancel", style:  .Default, handler: nil)

    let plLists = ["List1", "List2", "List3", "List4"]
    for(var i = 0; i < plLists.count; i++) {
        alertA.addAction(UIAlertAction(title: plLists[i], style: .Default, handler: self.handlerForAction)) 
    }

    alertA.addAction(action)
    self.presentViewController(alertA, animated: true, completion: nil)
}

func handlerForAction(action: UIAlertAction) {
    print(action.title!)
}

I hope this help you.

Upvotes: 1

Jack
Jack

Reputation: 81

I dont know why is it happening that way, So I Found out the Answer as an Alternative:

Using for loop in different way:

for titled in plLists{
 alertA.addAction(UIAlertAction(title: titled, style: .Default, handler: {
    action in

        NSLog(titled)

    }))
}
}

getting the correct output now, thanks for patience...

Upvotes: 0

Related Questions