How to show UITableViewController programmatically - ios

In my application, users can submit a new post similar to Instagram. However, if they decide not to post, they can press "Cancel" button on the UITabBarButton, and it will load the latest posts.
I've looked around for solution, however they are mainly UIViewController.
showViewController Not Working
However, what I need is to force open a UITableViewController. My codes are as follows, which I know it does not work, because it does not conform to the expected UIViewController.
#IBAction func cancelBtn(sender: UIBarButtonItem) {
self.navigationController?.pushViewController(LatestPostViewController as UITableViewController, animated: true)
}
EDIT: Added a screenshot for better illustration purposes. So the "Cancel" button is embedded in a Navigation Controller inside Camera Tab. I need to open up Latest Post tab when users press "Cancel" button on UINavigationBar
Apologies, I do not have enough reputation to display image directly.
Image

Check to make sure your cancel button action is set up correctly. In the action you want to transfer to the appropriate tab:
#IBAction func cancelBtn(sender: UIBarButtonItem) {
self.tabBarController?.selectedIndex = 0
}

Related

Button not performing segue

Trying to perform a segue "toLogin" when the login button is pressed. Segue is named and I control-dragged the button to the block of code to make sure they are associated. Very new to coding and not sure where I went wrong.
Currently, when button is pressed it changes color but does nothing. I also previously set it to print something when pressed but that never occurs either.
Screenshot of code, storyboard
For you to connect a button to a function, the function needs to look like below
#IBAction func login(_sender: Any) {
}
You will then need to connect to the blank circle next to the function on the left.
Regarding your screenshot, each segue linked to the whole main storyboard as highlighted showing. Not linked to button. Remove those two segues from storyboard first, and linked each segue to storyboard reference or next View Controller as attached image.
Make sure you have connected your button to the #IBAction in your UIViewControllerConnect IBAction to UIButton
Looks like you are triggering segue through UIBarButtonItem,
Swift 4.2,
loginButton.action = #selector(login(sender:))
#objc func login(sender: UIBarButtonItem) {
//Perform segue
}
Thanks for answering everybody. I'm still not sure what exactly my issue was but I decided to re-create the storyboard page without using UIBarItems and it worked. I'm guessing that something about the UIBar was preventing interaction.

How can i use the IBAction botton to go to the next storyboard using Swift?

Im trying to write a function that will take me from on storyboard to the next using a button. I am new to iOS app dev and Swift.
I have a button with this IBAction but im not sure how to connect pages. Meaning going to the next page using a button. thanks.
#IBAction internal func nextBtn(_ sender: Any)
}
Drag from the View in storyboard we’re you have the brutton on , crt drag from the yellow dot on the top of the view, to next view you want to navigate to. Give it a name in and put this in your button function.
// segueName is your name on segue
performSegue(withIdentifier: "segueName", sender: self)
Difference between drag it straight from the button or the view controller is that you can’t have any if statement if you drag it from the button.
Exp you only want to execute segue if user did something and the pressed the button you need to disable the button. Otherwise it will always be executed.
But if you use a performsegue in the button action you could easy make a statement and and execute the segue.
No need to write any code for this, it can all be done in Interface Builder. Create a storyboard reference in IB, and control-drag from the “next” button to the storyboard reference. Done.
There is a nice tutorial with pictures here:
https://medium.com/#wilson.balderrama/how-to-segue-between-storyboards-86c582f976f7

Changing view controllers Swift 3

I am learning ios development and am trying to make an app that has two views, each with a button that when pressed will send you to the other view. I have the two scenes set up in the storyboard and the button connected to an action. According to what I've read, using the instantiateViewController and an identifier should be enough to change the view. But when I run the simulator the first view appears but clicking the button has no effect. I dont't even get a error message.
#IBAction func SwitchViews(_ sender: UIButton) {
//Switch view controllers
let vc = storyboard?.instantiateViewController(withIdentifier: "second")
navigationController?.pushViewController(vc!, animated: true)
}
Have you embed you storyboard to navigation controller.In your code navigation controller optional so controller is not able get navigation controller.
you have to embed your first storyboard to navigation controller And it will work.You can do it by In Xcode>Editor>Embed In>Navigation Controller.
Just one suggestion if you are going to be developing apps that doesn't use navigation view what you have done is slightly off. The right way to do the connection to multiple views in a single view application is like this. I'll explain it in steps
Step 1 - Add a ViewController to your story board
Step 2 - Create a cocoa touch class from file - new - file (command + N will also do the trick)
!!! make sure its cocoa touch class !!!
Step 3 - keep subclass of as UIViewController enter the class name keep it unique to what the view does so the code looks clean and clear for everyone.
Step 4 - Hit next and finish. Now go to the story board and add the class name to the view controller you have created inside the identity inspector and add the storyboard identifier name as well
Step 5 - link your button to an action within your swift file as TouchUpInside and add the following code
#IBAction func startButtonPressed(_ sender: UIButton) {
let controllerVariable = self.storyboard?.instantiateViewController(withIdentifier: "storyboardIdentifier") as! ViewControllerClassName
self.present(controllerVariable, animated: true, completion: nil)
}
This should do the trick for creating single view applications and then building up your app with multiple screens.
But if your app uses IOS default navigation controller style like a nav bar with navigation items and a back button best practice is to use the nav controller.
If it doesn't then you have complete control over how you design your user interface by following the above steps.
Hope this helps :)

IOS Segue not appearing. No error

I am trying to implement a segue programmatically in IOS.
My story board looks like this:
The first segue between the first two screens is done by connecting the Login button to the next screen (so not completely programmatically), and it works fine.
The issue is with the "secondSegue", which I tried doing programmatically.
It simply does not work and there is no error message appearing.
I connected the 'Load this Screen' button to the following action in the Second Screen's view controller:
#IBAction func openThirdScreen(_ sender: Any) {
performSegue(withIdentifier: "secondSegue", sender: nil)
}
But the screen isn't opening and there is no error.
All the proper IBOutlets seem connected so I don't know what the issue could be.
I put a print statement in openThirdScreen(), and it prints, but a print statement in the third screen's View Controller's viewDidLoad() does not print, even though
I made sure the third screen was connected to a view controller.
I had overridden performSegue() earlier in order to do more with the segue but it was not working so I removed it for simplicity.
Please let me know what I messed up.
Sorry if this is a stupid question, I'm very new to XCode and I couldn't find anyone online with the same issue.
Let me know if there are more screenshots that are necessary to answer my question.
Edit: the following image was requested, in order to answer the question

Do something on button tap then segue to another view controller

I turned an image into a UIgesturerecognizer so that I can press it like a button. When I press it I want it to segue out, but the next view loads a camera so I'm getting kind of a freeze while it's loading. I can't really run an activity indicator because it's going between view controllers. So I made a label that says "please wait..." and I set it to appear on the button press before the segue. The problem is that it isn't actually appearing because the freeze is happening before it actually shows up. Then on the new vc you briefly see it flash, so it is appearing but not quick enough to be efficient. How can I make the label appear, then begin the segue? This is the code right now
#IBAction func buttonTapped(sender: AnyObject) {
//I want this label to show up before the segue starts happening
self.loadingLabel.hidden = false
//Segue
self.performSegueWithIdentifier("profilePicCamera", sender: self)
}
Call your camera load function from the viewDidAppear method.

Resources