Do something on button tap then segue to another view controller - ios

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.

Related

How to detect back button is pressed in the next viewController in a navigationController

I want to detect if back button is pressed in the next viewController in a navigationController.
Let's say I have VC_A and VC_B viewControllers in a navigationController. I know how to detect if back button is pressed in a current view controller but I do not know how to detect it in a previous viewController.
Edit:
I go from VC_A to VC_B and when I press back button in VC_B then I want to call a function in VC_A.
You could use notification center. This link has a nice tutorial: https://learnappmaking.com/notification-center-how-to-swift/
I want to detect if back button is pressed in the next viewController in a navigationController.
I'm not sure I understand this exactly, but it really doesn't matter much: in essence, you're talking about some view controller (call it controllerA), whose views aren't currently visible, finding out about a change that affects some other view controller (controllerB). The usual reason for needing such a thing is so that controllerA can update some data that it manages.
A better way to handle that is to have both controllers share a common data model. Any application state that's affected by something like a view controller being dismissed is shared data that should be part of the data model. controllerA really shouldn't care about whether controllerB's back button was tapped or not... that event is only the business of controllerB (and arguably the navigation controller that manages it). What controllerA should care about is updating its own views according to whatever changes happened while it was off screen, and those changes should be recorded in the model by controllerB and any other view controllers that might have been active along the way.
I'm suggesting you to do that with Notification Center like AglaiaZ suggested you. But if you're not feeling comfortable with using Notification Center, then try this more basic solution with viewWillAppear delegate method in viewController from which you're tracking are you back from B VC. So, let's go.
Set this variable in your current view controller class where you want to trigger method when the back button is pressed on the specific view controller, let's call that specific view controller B VC.
let isFromBViewController = false
Then in code block where you're triggering the transition to B VC set this variable to true.
func goToBViewController() { // This method is triggering transition from A VC to B VC
isFromBViewController = true }
And then in viewWillAppear delegate method check did current VC from which we triggered the transition to B VC have appeard from B VC.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isFromBViewController {
// code for doing something when you got back from B VC
isFromBViewController = false
}}
And that's it.
But, again I'm suggesting you to use the notification center as #AglaiaZ suggested, the tutorial is easy, and with that tutorial I've also learned how to use Notification Center and how to create and use custom notifications.
Good luck.
If I understood correctly, you want to do something when the back button in the navigation bar at the current view controller is pressed, and the user is going back from the current B view controller to A view controller.
Put this line of code in the view controller in which you want to track when the user has pressed the back button.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isMovingToParent {
//your code when back button is pressed
}
}

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

ViewController object recreated after navigate back?

Now I created a new project to test unwind segue.
In Storyboard the story entry is on VC0, a button in VC0 goes to VC1 (modally), a button in VC1 goes to VC2 (modally).
VC2 has a button to dismiss itself and in the function it looks like this:
#IBAction func btnDismiss(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
in VC1 I added an unwind function as follows:
#IBAction func unwindSecondView(segue: UIStoryboardSegue){
print("unwinded")
}
Then in storyboard I linked VC2's exit icon to unwindSecondview as action.
It works pretty well, however after I clicked "dismiss" button in VC2, VC1 appeared briefly and jumped back to VC0.
??? Anything wrong that caused jumping back to VC0?
--------------Initial question -----------------
I'm new to iOS and got a little confused for how VCs are created and activated.
Say I created 2 ViewControllers in my single view app. MainController(VC_M) and SettingsController(VC_S). I added the segues in storyboard, a setting button in VC_M goes to VC_S, VCS collects information, writes to standardDefaults, then ok button goes back to VC_M and refresh its view.
When I try to preserve some information in VC_M, I found that after I click ok button in VC_S and go back to VC_M, VC_M gets recreated and viewDidLoad() gets called. In the debugger, it shows the VC_M object (self) now has a different address in memory, so seems it's another instance of the class.
Is this a normal behavior? It's best that the old VC_M object gets saved so some data doesn't need to be reloaded. Initially I thought I could put some data loading stuff in init(), but when VC_M gets re-created init() got called, too. Some older posts suggested that ViewDidLoad() should not be called again but rather ViewDidAppear() should get called, but I'm not sure if it's the same case now.
In my case is it better to just use standardDefaults to reload everything, or is there a different kind of segue I should use? (I used push segue).

How to show UITableViewController programmatically

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
}

Resources