显示警报框子视图按钮后自动隐藏在swift中

时间:2016-09-08 07:42:54

标签: ios swift

我正在为表视图列表实现搜索功能。一切正常,但没有搜索数据发现我正在显示警报框。但是我打电话后警报框搜索按钮会自动隐藏。这是我的代码 -

   override func viewDidLoad() {
    super.viewDidLoad()
     month_names()
    search_params = ["International","National","State"]
    search_btn.hidden = true
    search_btn.enabled = false

    self.typePickerView.hidden = true
    self.typePickerView.dataSource = self
    self.typePickerView.delegate = self

    //other pickerView code like dataSource and delegate
    self.view.addSubview(typePickerView)
    self.view.superview?.addSubview(search_btn)
     }

从服务器加载数据后我做了这个 -

    search_btn.hidden = false
    search_btn.enabled = true
    self.view.superview?.addSubview(search_btn)

但是当没有找到搜索数据时我称之为有趣的显示提示框,此后 search_btn 会自动隐藏

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if(shouldShowSearchResults){
        if(Search_data.count > 0){
           return Search_data.count
        }
        else{
            shouldShowSearchResults = false
            set_no_data()
           return TableData.count
        }
    }
    else{
        return TableData.count
    }


}

    func set_no_data(){
    dispatch_async(dispatch_get_main_queue(), {
        self.show_alert_view("No Results Found!")
        self.search_btn.hidden = false
        self.search_btn.enabled = true
        self.view.superview?.addSubview(self.search_btn)
    })
}

我的提示框代码如下 -

       func  show_alert_view(message:String)  {
    let alertController = UIAlertController(title: "Alert", message:
        message, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Ok", style:   UIAlertActionStyle.Default,handler: nil))

    self.presentViewController(alertController, animated: true, completion:  nil)
}

请为此提供解决方案,提前谢谢......

1 个答案:

答案 0 :(得分:0)

哦,我得到了答案! 替换代码 -

self.view.superview?.addSubview(search_btn) with
self.view.addSubview(self.search_btn)