Swift - 编辑文本字段和点击按钮时发出警告

时间:2017-04-20 21:03:24

标签: ios swift performance warnings

我有一个滚动视图,可以通过编程方式添加UI元素,但是我添加的按钮似乎在模拟器和我用来测试的iPhone 7 Plus上都有一个响应延迟。我不确定为什么。每次编辑文本字段时,我也会收到警告消息。我该如何解决这些问题?在我们的警告信息和我的一些代码下面。

感谢您的帮助!

日志中的按钮警告:

2017-04-20 14:49:35.087232-0500 HAEForMe[889:369946] Warning: Attempt to present <UIAlertController: 0x102642b00>  on <AppName.UserSetupViewController: 0x10303dc00> which is already presenting <UIAlertController: 0x10243b9b0>

日志中的文本字段警告:

2017-04-20 16:00:56.105536-0500 HAEForMe[52504:6463817] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/andrewzimmerman/Library/Developer/CoreSimulator/Devices/476B95C5-691C-43D0-98D8-EAC400F6A41A/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-04-20 16:00:56.106290-0500 AppName[52504:6463817] [MC] 

代码:

//Begin UITextField Initialization
var inputFields = [UITextField]()
let textField = UITextField() // This continues until there are 10.

/*
 AddFields - Adds UITextFields to the inputFields array.
 */
private func addFields()
{
    inputFields.append(textField) // Done for all 10 fields.
}

/*
 ViewDidLoad - Runs stated code when the view loads.
 */
override func viewDidLoad() {
    super.viewDidLoad()
    prepareScrollView() //If view is touched, keyboard is dismissed.
    addFields() //Adds some UITextFields to an array.
    managePages() //Adds images, textfields, and button to ScrollView
    addDoneButtonOnKeyboard() //Adds a tool bar with the done button
}

/*
 ManagePages - Adds all items required for Scroll View [Some code
 is removed as it is irrelevant]
 */
private func managePages()
{
    self.sideScrollView.frame = CGRect(x:0, y:0, width:self.view.frame.width, height:self.view.frame.height)
    let scrollViewWidth:CGFloat = self.sideScrollView.frame.width
    let scrollViewHeight:CGFloat = self.sideScrollView.frame.height

    //*!*!* EFFICIENCY IN QUESTION *!*!*
    for (index,element) in inputFields.enumerated()
    {
        if(index == 0)
        {
            //Sets properties of UITextField
        }
        else if(index == 1)
        {
            //Sets properties of UITextField
        }
        // The else if statements continue on until index is 9
    }

    let getStartedButton = UIButton(frame: CGRect(x:((scrollViewWidth*5) + scrollViewWidth/2)-125, y: scrollViewHeight-175, width: 250, height: 99))
    getStartedButton.setImage(UIImage(named: "GetStartedButton"), for: UIControlState.normal)
    getStartedButton.addTarget(self, action: #selector(finishSetup), for: .touchUpInside)

    //*!*!* EFFICIENCY IN QUESTION *!*!*
    for fieldName in inputFields
    {
        self.sideScrollView.addSubview(fieldName)
    }

    self.sideScrollView.addSubview(getStartedButton)

    self.sideScrollView.contentSize = CGSize(width:self.sideScrollView.frame.width * 6, height:self.sideScrollView.frame.height)
    self.sideScrollView.delegate = self

}

/*
 FinishSetup - Checks all fields for input, ensure they aren't blank
 and uses them 
 */
func finishSetup(sender: UIButton!) {
    print("Finished setup button tapped, attempting to finalize setup.")

    var firstName: String = ""
    var lastName: String = ""
    var dateOfBirthValue: String = ""
    var dateFormatter: DateFormatter
    var date: Date = Date()
    var age:Double = 0.0
    var weight: Double = 0.0
    var height: String = ""
    var factType: String = "" //Name Changed
    var valueType: Int = 0 //Name Changed

    for (index,fieldName) in inputFields.enumerated()
    {
        if(fieldName.text != " " && fieldName.text != "" && fieldName.text != nil)
        {
            /*
             Goes through each field in the array via each and every index
             and then uses if statements to pull the text value for use in
             a database later on.
             */
        }
        else
        {
        //*!*!* EFFICIENCY IN QUESTION *!*!*
            print("Presenting blank field alert.")
            let alertController = UIAlertController(title: "Uh Oh!", message: "Looks like you left something blank, please make sure that you've filled every field out and try again.", preferredStyle: UIAlertControllerStyle.alert)
            let okayAction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default) {
                (result: UIAlertAction) -> Void in
                print("User reacted to blank field alert.")
            }

            alertController.addAction(okayAction)
            self.present(alertController, animated: true, completion: nil)
            return
        }
    }
}

0 个答案:

没有答案