Button in uitabbar doesn't push any view controllers swift 3 - swift

I've added a button as a subView to tabbarcontroller to open the camera and gallery. It works fine but whenever it needs to push a view controller it does nothing. I removed the function to call the camera and gallery, and it will simply print a text in the console and it works. Meaning the button works fine but when it comes to pushing/presenting VC, nothing..
Does it something to do with the button inserted as a subview to a tab bar? It is supposed to be one of those big buttons in the middle like in Instagram.
I already made the VC to be pushed as a subclass of UITabbarcontroller. Still nothing..
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
myPickerController.delegate = self
let sell_image = UIImage(named: "Sell icon")
button.setImage(sell_image, for: .normal)
button.setTitle("Sell", for: .normal)
self.view.insertSubview(button, aboveSubview: self.tabBar)
button.addTarget(self, action: #selector(sellVC(sender:)), for: .touchUpInside)
}
then the button function itself:
func sellVC(sender:UIButton!) {
print("this button prints this successfully but doesn't push the VC below")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "dummy") as! DummyViewController
self.navigationController?.pushViewController(vc, animated: true)
}

Related

How to set title of navigation item after open another view controller using with present modally - Swift

I have two view controllers in my application. Root view controller has some components and one of them is a button. That button provides to open another view controller using with present function. Second view controller(SelectTimeViewController) that opens when the button is tapped, was opened successfully. I am trying to set navigation title and items but I can not see them.
I did not use storyboard so root view controller is setting from AppDelegate.
let viewController = ViewController()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: viewController)
window?.makeKeyAndVisible()
When tapped the button, "openVC" function is invoked.
#IBAction func openVC(_ sender: Any) {
self.navigationController?.present(SelectTimeViewController(), animated: true, completion: nil)
}
I am trying to set title and rightBarButtonItem in SelectTimeViewController's viewDidLoad function but I can not see both of them.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "close"), style: .plain, target: self, action: #selector(closeIt))
self.navigationItem.title = "Select Time"
}
In additional to this, I can see both title and right bar button item when change the "openVC" function like as bellow.
#IBAction func openVC(_ sender: Any) {
self.navigationController?.pushViewController(vc, animated: true)
}
You have to push the SelectTimeViewController instead of present
Or if you really want to present it, you should present another UINavigationController that has the rootViewController as SelectTimeViewController()

Target Action to UIButton of nib not working in parentViewConteroller

I am trying to add targetAction to my UIButton of my subView.
Here is how I add UINib to my parentViewController.
let myView = UINib(nibName: "myNibName", bundle: .main).instantiate(withOwner: nil, options: nil).first as! CarTypeCollectionCell
myView.setData(param, secondParam)
self.mainView.addSubview(myView)
myView.button.addTarget(self, action: #selector(onClickBtn(_:)), for: .touchUpInside)
And here is my onClickBtn function.
#objc func onClickBtn(_ sender: UIButton) {
print("this is clicked")
}
But the button action is not working. I set all of my button and view isUserInteractionEnabled to true.
When I tried with UITapGesture to myNibView, its working pretty well. But not with in that button addTarget case.
Is there something I am missing?

swift present controller with a back button to get back to last screen

I am presenting a UICollectionViewController but it just ends up as a controller with no back buttons to get back to the last controller or the home controller, how would i add a back button? or what other options do i have?
class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate, ProfileHeaderClosedDelegate {
func presentFRC() {
let PE = FriendsRequestsController(collectionViewLayout:
UICollectionViewFlowLayout())
let NPE = UINavigationController(rootViewController: PE)
self.present(NPE, animated: true, completion: nil)
}
}
Add the following code in the view did load of the view controller you are presenting.
override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal)
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
self.navigationController?.popViewControllerAnimated(true)
}
You have two options:
Embed your view controllers into UINavigationController and use UINavigationBar and segues for navigation. The easiest way to build such a system would be through storyboard though. But you can do that programmatically as well, but it would be a lot more work.
Add your own back button programmatically the way #ZahurafzalMirza has shown you. Since you are not using UINavigationController, your navigationController will be nil. So the action should look like this
func backAction() -> Void {
self.dismiss(animated: true)
}

Calling a NavigationController (to push another view controller) from a CollectionViewCell

Here is what I'm trying to do.
class UserProfileHeader: UICollectionViewCell {
lazy var button: UIButton = {
let button = UIButton(type: .system)
//...
button.addTarget(self, action: #selector(handleButtonTapped), for: touchUpInside)
return button
}()
func handleButtonTapped() {
print("action working now")
// How to push another vc here?
}
// ...
}
I want to call a navigationController to push(show) another ViewController here in the collectionViewCell. How can I access there? I tried, not worked. Is it because of MVC pattern? Cell can't talk with others? Or is there any ways to make it? like using Delegate/Protocol? I don't know much about those things.
Create one custom delegate and set to its viewcontroller and then when you tap on collectionview cell button called that delegate method and it will invoke inside your viewcontroller there you can push the view controller
func handleButtonTapped() {
//Your delegate method showuld be invoked here
delegate?.buttontappFromCollectionViewCell()
}
//Now inside your view controller
func buttontappFromCollectionViewCell() {
let vc = UIStoryboard(name: "storyboardName", bundle: nil).instantiateViewController(withIdentifier: "yourViewControllerIdentifier")
let navigationController = UINavigationController(rootViewController: vc)
navigationController.pushViewController(vc, animated: true)
}

Can't Click on UITabelCell after implementing UIRefreshControl

I have recently implemented the RefreshControl on my ViewController which contains UITableDataSource and Delegate. When you click on each cell of the table, it leads to other view controller but after implementing the refresh control, I can no longer click on each cell. Is it because I added the subview(refreshControl) to the view?
Here is the code below,
tableViewController.refreshControl = self.refreshControl
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: Selector("refresh:"), forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
func refresh(sender:AnyObject) {
// Code to refresh table view
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}