Swift 4 pop to a view controller : navigation method [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Pop To viewController : Go to Previos controller
self.navigationController?.popToViewController(AddSalesController, animated: true)

Make sure your controller is in the navigation stack and you can try this code.
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: SOListScreen .self) {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}

Related

Swift textfield text 1 to 9 if user entered need to show alert [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying simple logic, I have only one textfield. into this textfield if user entered 1 to 9 I need to show alert using if.
if emailTextField.text < 0 {
print("100%")
}
UITextField's text is always a String. So if you need to check if it is a particular number, you need to cast the String value to an Int.
if let number = Int(emailTextField.text!), (0...9).contains(number) {
print("100%)
}
Note: You might want to check out the UITextFieldDelegate methods if your logic revolves around text field editing events.
yourTextField.addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged)
.
.
.
#objc func valueChanged(_ textField: UITextField) {
if let number = Int(emailTextField.text!), (0...9).contains(number) {
print("100%)
}
}
Sidenote: I have no idea what your use case is for this.

iOS MVVM how to pass data from child to parent view model [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to MVVM. How should I update the data model in parent view model from child view model?
As an example, suppose I have a CollectionViewModel for a Newsfeed page, it keeps an array of Post (my date model) and an array of CollectionCellViewModel that corresponds to individual posts. Each cell makes network request to listen for new likes. How can I pass this information back to CollectionViewModel and update Post?
You achieve that with multiple ways:
Delegation pattern:
protocol CollectionCellViewModelDelegate {
func onAction()
}
class CollectionCellViewModelDelegate: YourType {
var delegate: CollectionCellViewModelDelegate?
/// Your custom action
func yourCustomAction() {
delegate?.onAction
}
}
then assign .delegate in your parent class and implement onAction() method
Closures:
class CollectionCellViewModelDelegate: YourType {
var yourAction: (()->())?
func yourAction(_ completion: (()->())?) {
yourAction = completion
}
/// Your custom action
func yourCustomAction() {
yourAction?()
}
}
then call your closure with following code from parent class:
child.yourAction { // do custom stuff }

Have a Controller for Each Cell of a UICollectionView [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a UICollectionView with 4 Cells, I have a controller for those 4 pages called SwipingController.
I'd like to have a UIViewController for each cell, how can I achieve that ?
I counter advise on what you are going for.
Better just to have Views that manage their layout you don't need controllers to manage layout.
I don't think you will be able to hold a familiar architecture (MVC/MVVM/MVP/VIPER .. ) in the future if you go in this direction.
Also check this out. It will help you understand better the role of a view controller.
Nonetheless you can achieve this with addChildViewController(documentation here)
Add child view
let controller = storyboard?.instantiateViewController(withIdentifier: "test") as! UIViewController
self.addChildViewController(controller)
controller.view.frame = CGRect(0, 44, 320, 320)
self.view.addSubview(controller.view)
controller.didMove(toParentViewController: self)
Remove child view
let vc = self.childViewControllers.last
vc.removeFromSuperview()
vc.willMove(toParentViewController: nil);
vc.removeFromParentViewController()

Perform Segue From Another Swift File via a class function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can I use a class function I implement to perform a segue if the class it is held in is not connected to a viewController?
In order to perform a segue, you need the instance of view controller where the segue originates from - for example:
class MyViewController {
}
class MyClass {
showNextView(fromViewController: UIViewController) {
fromViewController.performSegueWithIdentifier("segue_id", sender: fromViewController)
}
}
However I discourage this kind of interaction. Don't tell the view controller how to show a view - let it do on its own, and just ask it.
So, I suggest creating an instance method in your view controller:
class MyViewController {
...
func goToNextView() {
performSegueWithIdentifier("segue_id", sender: self)
}
}
and use that to make the transition:
class MyClass {
showNextView(fromViewController: MyViewController) {
fromViewController.goToNextView()
}
}
You can instruct the currently visible view controller to segue using "performSegueWithIdentifier".
You obviously need a reference to the visible controller.
Inside your method use:
currentViewController.performSegueWithIdentifier("push", sender: currentViewController)

Implement Objective C code in Swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to handle the pressing of the back button on a UI Navigation bar. I have an extension in objective C (https://github.com/onegray/UIViewController-BackButtonHandler) and i have bridged it to my project in Swift using the header, now I do not quite know how to implement the code in Swift. This is the implementation in C:
-(BOOL) navigationShouldPopOnBackButton {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return NO; // Ignore 'Back' button this time
}
return YES; // Process 'Back' button click and Pop view controler
}
func navigationShouldPopOnBackButton() -> Bool {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return false // Ignore 'Back' button this time
}
return true // Process 'Back' button click and Pop view controller
}

Resources