Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按下ViewController后,parentVIewController中的按钮仍显示在屏幕上_Ios_Swift_Uinavigationcontroller - Fatal编程技术网

Ios 按下ViewController后,parentVIewController中的按钮仍显示在屏幕上

Ios 按下ViewController后,parentVIewController中的按钮仍显示在屏幕上,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,我是IOS的新手,我陷入了这个奇怪的问题 这真是一个简单的演示,我只想跳到另一个viewController //parentViewController import UIKit import SnapKit class ViewController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after load

我是IOS的新手,我陷入了这个奇怪的问题 这真是一个简单的演示,我只想跳到另一个viewController

//parentViewController
import UIKit
import SnapKit

class ViewController: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = UIColor.whiteColor()
    let button = UIButton()
    button.frame = CGRectMake(100, 100, 200, 100)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Search Button", forState: UIControlState.Normal)
    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
}

func buttonAction(sender:UIButton!){
    print("Button tapped")
    let searchVC = MfSearchViewController()
    self.pushViewController(searchVC, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
这是目标viewController

class MfSearchViewController:UIViewController{
    /// UI component
    var tableView:UITableView!
    var searchBar:UISearchBar!
    var searchLabel:UILabel!
    ///
    var dataSource = NSMutableArray()
    var currentIndexPaht: NSIndexPath?
    var screenObj = UIScreen.mainScreen().bounds




    override func viewDidLoad() {
        super.viewDidLoad()
        for index in 0...12{
            let model = SearchResultModel(searchWord: "java\(index+1)", searchResult: "1500")
            dataSource.addObject(model)
        }
        self.view.backgroundColor = UIColor.lightGrayColor()
        tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
        //tableView = UITableView(frame: CGRectMake(0,20,screenObj.width,screenObj.height), style: UITableViewStyle.Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        self.title = "target"


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MfSearchViewController: UITableViewDataSource{

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier: String = "SearchResultCellIdentifier"
        // may be no value, so use optional
        var cell: SearchResultCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? SearchResultCell
        if cell == nil { // no value
            cell = SearchResultCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
        }
        let model: SearchResultModel? = dataSource[indexPath.row] as? SearchResultModel
        cell!.configureCell(model)
        return cell!
    }

    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

   }

extension MfSearchViewController: UITableViewDelegate{
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print(indexPath);
    }

}
但在按下ViewController之后,parentViewController的按钮仍然存在…请帮助我…非常感谢


我不是UIViewController和故事板方面的专家,但现在您只需在更改ViewController之前删除按钮即可

 button.removeFromSuperView()
您必须使按钮成为全局属性,以便可以在创建它的ViewDidLoad之外调用它。
我不确定这是否是预期行为,或者按钮是否应自动删除。

您正在将子视图添加到UINavigationController的视图中。这样做将导致视图出现在其视图控制器的所有后续屏幕上。通常不需要将视图添加到UINavigationController的视图中。相反,您应该创建动态推送到导航控制器堆栈上的视图控制器。也就是说,创建一个根视图控制器,其按钮调用执行以下操作的操作:

let searchVC = MfSearchViewController()
self.navigationController.pushViewController(searchVC, animated: true)

在打开新视图转换时,关闭按钮转换视图。只需调用
按钮。removeFromSuperView()
将导致按钮消失,但会非常突然,不符合iOS用户体验或设计模式。

我知道您得到了答案,但也许我可以发现一些有关视图之间转换的技巧

self.pushViewController(searchVC, animated: true)
“self”表示您在
UIViewController
对象中,并且
UIViewController
将推动它。所以,例如,如果您从A推送类B,在B类中,您将无法在导航堆栈的控制下根目录任何内容。此外,您不在
UINavigationController
堆栈中

self.navigationController.pushViewController(searchVC, animated: true)

现在,您正在使用
UIViewController
对象
UINavigationController
并能够处理B类上的导航堆栈。这是推送、呈现或处理
UIView
对象之间的每个转换的真正方法。

!谢谢你的帮助,我真的需要更多的阅读和学习:)