如何在AlertController块(Swift)中放入大块代码

时间:2015-10-10 20:36:08

标签: swift parse-platform uialertcontroller

在我的集合视图单元格中,我有一个按钮,可以将当前用户(PFUser)成功保存到每个事件的用户数组(索引路径中的对象)。

但是,我想添加一个警报视图,但是在将大量代码放入警报操作块中时未能成功。我想我可以创建另一种方法,但我觉得有一种更简单的方法。

当我尝试把它放进去的时候,A。支架的数量太多让我失望了B.它没有认识到" alertController"在方法的底部。

感谢您的帮助。

func buttonTapped(sender: AnyObject) {

    let button = sender as! UIButton
    let view = button.superview
    let cell = view?.superview as! EventCell
    let indexPath = collectionView.indexPathForCell(cell)

//        print(indexPath)

    if let indexPath = indexPath {
        if let event = events?[indexPath.row] {

            //pops alert vc

            var alertController : UIAlertController = UIAlertController(title: event.eventTitle, message: nil, preferredStyle: UIAlertControllerStyle.Alert)

            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

            alertController.addAction(cancelAction)

            let saveUserToEvent = UIAlertAction(title: "Yes", style: .Default) { _ in



                // WHAT TO DO HERE? I want to add the stuff below


            }

            alertController.addAction(saveUserToEvent)

            var attendees = [String]()
            if let attendeesTmp = event["attendees"] as?[String] {
                attendees = attendeesTmp;
            }
            if let objId = PFUser.currentUser()?.objectId {
                var found = false
                for objIdd in attendees {
                    if objIdd == objId {
                        found = true
                        break;
                    }
                }
                if !found {
                    attendees.append(objId)
                    event["attendees"] = attendees;
                    event.saveInBackground()
                }
            }

            if let user = PFUser.currentUser() {
                var eventsAttending = [String]()
                if let eventsAttendingTmp = user["eventsToAttend"] as?[String] {
                    eventsAttending = eventsAttendingTmp;
                }
                if let eventId = event.objectId {
                    var found = false
                    for eventIdd in eventsAttending {
                        if eventIdd == eventId {
                            found = true
                            break;
                        }
                    }
                    if !found {
                    eventsAttending.append(eventId)
                    user["eventsToAttend"] = eventsAttending;
                    user.saveInBackground()
                    }

                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

你放了

alertController.addAction(saveUserToEvent)

在括号中也是如此?它呆在外面。

这应该有效

    func buttonTapped(sender: AnyObject) {

    let button = sender as! UIButton
    let view = button.superview
    let cell = view?.superview as! UITableViewCell
    let indexPath = collectionView.indexPathForCell(cell)

    //        print(indexPath)

    if let indexPath = indexPath {
        if let event = events?[indexPath.row] {

            //pops alert vc

            var alertController : UIAlertController = UIAlertController(title: event.eventTitle, message: nil, preferredStyle: UIAlertControllerStyle.Alert)

            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

            alertController.addAction(cancelAction)

            let saveUserToEvent = UIAlertAction(title: "Yes", style: .Default) { _ in



                var attendees = [String]()
                if let attendeesTmp = event["attendees"] as?[String] {
                    attendees = attendeesTmp;
                }
                if let objId = PFUser.currentUser()?.objectId {
                    var found = false
                    for objIdd in attendees {
                        if objIdd == objId {
                            found = true
                            break;
                        }
                    }
                    if !found {
                        attendees.append(objId)
                        event["attendees"] = attendees;
                        event.saveInBackground()
                    }
                }

                if let user = PFUser.currentUser() {
                    var eventsAttending = [String]()
                    if let eventsAttendingTmp = user["eventsToAttend"] as?[String] {
                        eventsAttending = eventsAttendingTmp;
                    }
                    if let eventId = event.objectId {
                        var found = false
                        for eventIdd in eventsAttending {
                            if eventIdd == eventId {
                                found = true
                                break;
                            }
                        }
                        if !found {
                            eventsAttending.append(eventId)
                            user["eventsToAttend"] = eventsAttending;
                            user.saveInBackground()
                        }

                    }
                }


            }

            alertController.addAction(saveUserToEvent)


        }

    }

}