Effect I Wish to Replicate
Example 1) https://youtu.be/EgU7BfOJ3BE
Example 2) https://youtu.be/kfBzZeGRSa0
In both examples, the user taps on a bar button item located in the navigation bar, and the List view controller segues to the Map view controller using the Flip Horizontal transition.
I have tried using container views, but cannot seem to get this working (I always get a full screen transition, rather than only the container view transitioning).
Any help is greatly appreciated!!!
Edit
Here is my attempt so far (link to the full Xcode project).
ParentVC (one with navigation bar):
import UIKit
protocol ButtonPressedDelegate
{
func buttonPressed(passedData:Bool)
}
class ParentViewController: UIViewController
{
var delegate:ButtonPressedDelegate? = nil
var switchValue:Bool = true
var childViewController:ChildViewController?
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func switchButtonPressed(_ sender: Any)
{
if switchValue
{
switchValue = false
}
else
{
switchValue = true
}
childViewController?.buttonPressed(passedData: switchValue)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "parentToChild"
{
childViewController = segue.destination as? ChildViewController
childViewController?.buttonPressed(passedData: switchValue)
}
}
}
ChildVC:
import UIKit
class ChildViewController: UIViewController, ButtonPressedDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func buttonPressed(passedData: Bool)
{
if passedData
{
print("Show Blue")
performSegue(withIdentifier: "showBlue", sender: self)
}
else
{
print("Show Red")
performSegue(withIdentifier: "showRed", sender: self)
}
}
}
Related
I am looking to pass data through a text box, and into an array (Second View Controller), so it can be added to a pool of data and randomly chosen.
For instance, you would input your name (and/or another name) into the text field in the First VC and and it would go into an array that would tell them which color they are shown in the Second VC.
Essentially, I'm trying to pass the data to a variable (which will hold all the names inputed), and append the variable to the array, then click a button to generate and randomly pull from the array via arc4random. My problem is its not appending. Any help would be greatly appreciated.
Here is my code:
VC 1:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func StartTapped(_ sender: Any) {
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
#IBOutlet weak var AddPlayerTextField: UITextField!
#IBAction func AddTexttoArrayBtn(_ sender: Any) {
if AddPlayerTextField.text == nil
{
performSegue(withIdentifier: "SecondViewControllerSegue", sender: self)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let SecondViewController = segue.destination as! ViewController2
SecondViewController.myString = AddPlayerTextField.text!
VC 2:
import UIKit
class ViewController2: UIViewController {
var myString = String ()
var TeamBuildingQuestions = [ "is Red", "is Blue", "is Green", "is Purple","is Teal","is Orange", "is Red", "is Grey", "is Pink"]
var MasterTeamBuildingQuestionsArray = [ "Hello", "beep bop bbeep", "Boop", "Bap","Bospop","bob the builder"]
#IBOutlet weak var DisplayArray: UILabel!
#IBAction func NextQuestionBtn(_ sender: Any) {
let RandomArrayNumber = Int (arc4random_uniform(UInt32(TeamBuildingQuestions.count)))
let QuestionDisplayed = TeamBuildingQuestions[RandomArrayNumber]
DisplayArray?.text = QuestionDisplayed
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
TeamBuildingQuestions.append(myString)
}
In VC2, Instead of didReceiveMemoryWarning, implement TeamBuildingQuestions.append(myString) in viewDidLoad.
Here is a bug:
if AddPlayerTextField.text == nil{
}
should be
if AddPlayerTextField.text != nil && AddPlayerTextField.text.count > 0{
}
I'm making a modal view that presents when the user touch up on a button. At the moment, I already show the modal and I can use that modal interface.
Now I wan't to dismiss the modal when a button inside the modal is pressed and after that do a segue from it's parent to another view.
let summary = SummaryViewController(nibName: "SummaryViewController", bundle: nil)
summary.preferredContentSize = CGSize(width: 800, height: 300)
summary.modalPresentationStyle = UIModalPresentationStyle.formSheet
self.present(summary, animated: true, completion: nil)
SummaryViewController have the following code:
import UIKit
class SummaryViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var summaryTV: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
summaryTV.delegate = self
summaryTV.textColor = UIColor.lightGray
summaryTV.text = "Escriba el resumen de la visita"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == UIColor.lightGray {
textView.text = nil
textView.textColor = UIColor.black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.textColor = UIColor.lightGray
textView.text = "Escriba el resumen de la visita"
}
}
#IBAction func saveSummary(_ sender: Any) {
print("SummaryTV: \(summaryTV.text)")
}
// MARK: Segues
// Overrided functions for segues. Ordered according to segue flow.
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// If false, segue cancelled. Segue identifier is a parameter of the function.
// If you use function 'performSegue()' flow do not pass this function.
if !Reachability.isConnectedToNetwork() {
CommonAlerts.inetAlert(vc: self)
return false
}
return true
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}
So when the user press the button inside the modal view, the IBAction saveSummary is triggered. In that moment I want the parent view controller who present the modal to capture the text on the modal and dismiss the modal. How can I do it?
#IBAction func saveSummary(_ sender: Any) {
print("SummaryTV: \(summaryTV.text)")
self.presentingViewController.dismissViewControllerAnimated(false,
completion: nil)
}
As for communication, you can setup a weak delegate to presenting VC which will implement a protocol:
protocol SummaryResult {
func saveResult(summary: String)
}
So I have the following layout for my iOS app.
What I'm intending to do is put a table view in the purpleVC to control the Green viewcontroller...the top peachVC will have text in it which will need to change. I'm just not sure how to control one view controller from another. This includes having the purple slide in and out when a button on the GreenVC is clicked. I know there are classes out there to do this however I want to learn as well.
TESTING DELEGATES:
MAINVIEW CONTROLER
import UIKit
protocol Purpleprotocol {
func buttonpressed()
}
protocol Greenprotocol {
}
extension UIViewController {
func alert(message: String, title: String = "") {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
}
class MainViewController: UIViewController,Purpleprotocol,Greenprotocol {
weak var infoNav : UINavigationController?
weak var greenVC: GreenVC?
weak var purpleVC: PurpleVC?
weak var peachVC: PeachVC?
func buttonpressed() {
alert(message: "This is message")
print("buttonpressed")
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
greenVC?.greenlabel.text = String(hour) + ":" + String(minutes)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "contentSegue" {
let infoNav = segue.destination as! UINavigationController
}
}
}
PURPLEVIEW CONTROLER
class PurpleVC: UIViewController {
var delegate: Purpleprotocol?
#IBAction func butclick(_ sender: UIButton) {
alert(message: "infunction")
delegate?.buttonpressed()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Thanks
R
It does depend on the case but to see a few examples:
A) You can connect it through the delegates. Your main view controller has 3 child view controllers to which it should report changes. It should also assign itself as a delegate to all 3 child controllers where it will get all notifications for events. This would look like
func purpleViewController(sender: PVC, selectedItem: Item) {
self.greenViewController.showItem(item: selectedItem)
self.peachVC.showItem(item: selectedItem)
}
func purpleViewController(sender: PVC, shouldSetMenuClosed closed: Bool) {
self.menuConstraint.constant = closed ? targetWidth : 0.0
}
B) You may have a data model which controls the whole screen and has a delegate for each of the children. The model will report any changes to its delegates so they may react accordingly. Main view controller would create an instance of this model when it loads and pass it to all of the children view controllers. The children would then directly manipulate the model:
In green controller:
func onTap() {
mode.menuShown = !mode.menuShown
}
In model:
var menuShown: Bool = true {
didSet {
self.menuDelegate.model(self, changedMenuShownStateTo: menuShown)
}
}
In main view controller:
func model(_ sender: Model, changedMenuShownStateTo shown:Bool) {
self.menuConstraint.constant = shown ? 0.0 : targetWidth
}
C) You can use notifications where any of the controllers may post to notification center a custom notification and other controllers may observe the notifications and act accordingly.
There are many other ways in doing so but these probably most popular. See if any of them fits you...
Delegation.
Your MainViewController will become a delegate to each of the embedded VC's that want to pass back information. From your description you'll need two delegate relationships:
protocol PurpleProtocol {
func selected(row: Int, text: String)
}
protocol GreenProtocol {
func slideButtonPressed()
}
Have MainViewController implement these protocols. Give identifiers to the embed segues. You can find them in the Document Outline view. In prepareForSegue, retain pointers to the embedded VC's and pass your self as the delegate:
class MainViewController: UIViewController, PurpleProtocol, GreenProtocol {
weak var greenVC: GreenViewController?
weak var purpleVC: PurpleViewController?
weak var peachVC: PeachViewController?
func selectedRow(row: Int, text: String) {
// do something useful
}
func slideButtonPressed() {
// slide purple view in or out depending on current state
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "EmbedPurple" {
let dvc = segue.destination as! PurpleViewController
purpleVC = dvc
dvc.delegate = self
}
else if segue.identifier = "EmbedGreen" {
let nav = segue.destination as! UINavigationController
let dvc = nav.topViewController as! GreenViewController
greenVC = dvc
dvc.delegate = self
} else if segue.identifier = "EmbedPeach" {
peachVC = segue.destination as! PeachViewController
}
}
}
In your embedded VC's, add a delegate pointer and call the delegate with the protocol method when it is time:
class GreenViewController: UIViewController {
weak var delegate: GreenProtocol?
#IBAction slideButtonPressed(sender: UIButton) {
delegate?.slideButtonPressed()
}
}
class PurpleViewController: UITableViewController {
weak var delegate: PurpleProtocol?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.selected(row: indexPath.row, text: modelArray[indexPath.row])
}
}
Ok so I am trying to make it when the user flips the cheat mode switch it will disable the answerButton. I did try to set the button to disable near the bottom however it does not appear to do anything. I added the print("") to see if it would print in the console however nothing was printed. I a unsure what is wrong here.
I have pasted my code below.
import Foundation
import UIKit
class SettingsController: UITableViewController {
#IBOutlet weak var cheatSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
cheatSwitch.isOn = UserDefaults.standard.bool(forKey: "switchState")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveSettings(_ sender: UISwitch) {
UserDefaults.standard.set(cheatSwitch.isOn, forKey: "switchState")
UserDefaults.standard.synchronize()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let mainView : ViewController = segue.destination as! ViewController
if cheatSwitch.isOn == true {
print("turn off button")
mainView.btnAnswer.isEnabled = false;
}
else {
print("turn on button")
mainView.btnAnswer.isEnabled = true;
}
}
}
You are saving the value UISwitch's state in UserDefaults so you can use its value in ViewController's method viewDidApper.
override func viewDidAppear(_ animated: Bool) {
self.btnAnswer.isEnabled = !UserDefaults.standard.bool(forKey: "switchState")
}
Note: There is no need to add prepareForSegue in your SettingsController because it will never called when you press back button of NavigationController so consider to remove it.
I am creating an iOS application in which I want to perform an assignment action in button press before running the method prepareForSegue.
I created all controls using Main Story board.
The order of execution for some buttons is
button press action -> prepareForSegue
but for some buttons it is
prepareForSegue-> button press action
How to change the order for second set of buttons?
Here is the code I am using:
import UIKit
class SummaryViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var button_prsd = "none"
#IBAction func people_insights(sender: AnyObject) {
button_prsd = "people_insights"
}
#IBAction func industry_research(sender: AnyObject) {
button_prsd = "industry_research"
}
#IBAction func holidays_events(sender: AnyObject) {
button_prsd = "holidays_events"
}
#IBAction func monthly_spotlights(sender: AnyObject) {
button_prsd = "monthly_spotlights"
}
#IBAction func blog(sender: AnyObject) {
button_prsd = "blog"
}
#IBAction func about_us(sender: AnyObject) {
button_prsd = "about_us"
print("button press about us executed")
}
#IBAction func settings(sender: AnyObject) {
button_prsd="settings"
print("button press settings executed")
}
#IBAction func quiz(sender: AnyObject) {
button_prsd="quiz"
print("button press quiz executed")
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let next_view = segue.destinationViewController
if(next_view is DetailViewController)
{
let det_view = next_view as! DetailViewController
det_view.link = button_prsd
print("segue executed")
} else if(next_view is DetailTableViewController)
{
let det_view = next_view as! DetailTableViewController
print("segue executed")
}
}
}
I figured out the problem, when give then code in this way, the execution is just alphabetical order, all the other methods were getting executed before prepareForSegue because the methods come above in alphabetical order
When I renamed the quiz and settings methods as a_quiz and a_settings, it worked