Swift: set Action for previous ViewController - iphone

i have 2 viewControllers the first one is to allow for user to add ad details and second viewController the rules and i want to dismiss the sec vc and automatically upload the ad at first vc when user tap agree at second vc in Swift 4.2

You can use callbacks, a protocol or a block.
Or you can always use UserDefaults and ask the first vc to check the value of your key every time it appears.
It really depends on what you are trying to achieve.

Related

how to change current viewcontroller if user do not login in swift?

i have an ios app with swift
i want to check current user in every ViewController
and if user do not login change ViewControllerto to LoginViewController
whate where do that?
Check if user is logged in at viewDidLoad, if not, set the login screen as the root: https://developer.apple.com/documentation/uikit/uiwindow/1621581-rootviewcontroller
If you want it work this way for every ViewController in your app, you can subclass UIViewController, and all your ViewControllers will inherit from that subclassed ViewController.
EDIT: as someone pointed in the comments, it's probably better to check this when the app is launched, or back from background (if you're worried the login session might expire): https://developer.apple.com/documentation/uikit/core_app/managing_your_app_s_life_cycle?language=objc

Come back to the tabBarController, swift

Currently on my viewController : Upload, my button send the data to my database only if all the information are filled out, and I come back to the preview view (table View) with :
self.navigationController?.popViewControllerAnimated(true)
I would like, if it is possible, to come back to my main view on the tabBarController. I tried many things, like directly on the storyboard with Present modally segue to "TabBar controller", but I come back to the TabBar without sending my data to the database and without checking in the information are filled out..
How can I do it?
Thanks!
UITabBarController has a property selectedIndex with which you can switch the selected tab. So on completion after dismissing the UploadViewController you can run:
self.tabBarController?.selectedIndex = 0 // Index to select
It would probably be best to create a delegate for your UploadViewController to fire a function to do all the work in your previewVC on API call completion.
(Super late response...in case someone has similar questions, presumably in later version of Swift, such as mine which is Swift 5, iOS 13.2). Steps:
Be sure to set an id for your UITabBarController storyboard, e.g. "TabBarViewController"
Next, add the following to an action that has already been connected to a button:
let ID_TABBAR = "TabBarViewCOntroller"
#IBAction func returnToTabbar(_ sender: Any) {
let tabBarController = self.storyboard?.instantiateViewController(identifier:ID_TABBAR) as! UITabBarController
self.navigationController?.pushViewController(tabBarController, animated:true)
}
Referenced from one of the responses from this post.
Update: In case your Tab Bar View Controller also happens to be the root view controller, the two lines of the code in the returnToTabbar method above can be:
self.dismiss(animated:true, completion:nil);
self.navigationController?.popViewController(animated:true);
(ref.: See answer here, for Swift4 but works just fine in Swift5)

How can be used phone locks as a action trigger in Swift?

I want to change to another viewcontroller in swift when the phone is locked. Is there any variable or function?
When the user locks their phone 'applicationWillResignActive' and then 'applicationDidEnterBackground' from the App Delegate get called and then when the user unlock it - 'applicationWillEnterForeground' and 'applicationDidBecomeActive' get called. You can either implement your view controller to be pushed in the App Delegate or add a notification observer in your view controller to react to any of these and push/pop/modally present the view controller you need. It all depends on your implementation - it is a bit of a broad question. Hope this helps!

How to load a UIViewController conditionally using storyboard and xcode 4.2

I have a screen in which a user can choose a set of meals - once the meals have been chosen the application fetches results form a database and displays a list of them. Now, I would like to implement a condition to decide whether the next screen should be loaded or not - ie. if there's no internet connection then show an alert and don't display the next screen etc.
I've implemented a system to check whether there is an internet connection or not but I'm not sure how and where to decide of the next screen should be loaded. Any ideas?
Thanks,
1.5 other options:
If you're willing to split stories and nibs, just load up a nib when you want/need to.
If you want to stick to stories exclusively, just load up another story when you need to. Same thing as loading a nib:
UIStoryboard *otherStoryboard = [UIStoryboard storyboardWithName:#"OtherStory" bundle:nil];
UIViewController *otherController = [otherStoryboard instantiateInitialViewController];
[self.navigationController pushViewController: otherController animated:YES];
Once you know in your code whether you want to display the next screen or not, can you not just add an if statement that either loads the next screen or displays a warning that there is no connection?
if (hasConnection) {
// Show next screen
} else {
// Show warning
}
You supposedly have an action that is being fired when the user selects some meals, haven't you? In this action you'd call [UINavigationController pushViewController:nextViewController animated:YES] or something like this. Put this function call into the condition of your preference, and show a popup otherwise.
I solved this issue using the answers from:
Prevent segue in prepareForSegue method? by linking the segue to my main view controller, then attaching an IBAction to the button that was originally the segue initiator and performing the logic in that method. If it all cleared then I call [self performSegueWithIdentifier:#"results" sender:self];

iOS5 - manual Storyboard view selection in code

I am new to iOS and using storyboards for the first time. When my app starts it checks back with the a server app I have written to see if the saved credentials are authenticated and I then in my AppDelegate class I then attempt to show the appropriate scene in the app's storyboard - MainMenu if authenticated or a Login Screen if not authenticated.
I have tried using instantiateViewControllerWithIdentifier on the storyboard and also the performSegueWithIdentifier on the initial NavigationController which is set to be the "Initial View Controller" to display the appropriate view..
However with both methods only the blank navigation bar shows and I am unsure where to go from here.
If there was some example code on how others manually manipulate storyboard scenes and viewcontrollers that would be great. Am I maybe putting the code in the wrong place (ie should it go into the first View Controller) or should that not matter? No exceptions are raised and I seem to have access to instantiated objects as required.
I am thinking I need to understand the operation of the app delegate's window more, or maybe should I focus on manually loading the storyboard by removing it's reference from the InfoPlist settings?
Any thoughts would be greatly appreciated.
From my (admittedly haphazard) understanding of storyboards (at the moment), you should have two named segues going from a first viewcontroller, and then you can simply trigger one or the other as need be (I presume there's some sort of "loading/authenticating" screen, however brief?)
if (success) {
[self performSegueWithIdentifier: #"MainMenuSegue" sender: self];
} else {
[self performSegueWithIdentifier: #"LoginSegue" sender: self];
}
To debug, I'd set up buttons on the initial viewcontroller just to be sure the segue linkings/etc are proper.
You really shouldn't need to instantiateViewControllerWithIdentifier unless you're working around segue/storyboard limitations. I think.
I've put the performSegueWithIdentifier in my app's first viewcontroller's viewDidAppear (not the best idea, I think; but that's sort of the soonest it should happen? and I would hedge towards saying it should be triggered somewhere in the viewcontroller stack, not from the appdelegate, but I haven't tested that).