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)
}
Related
import UIKit
class ViewController: UIViewController {
// #IBAction func Btndel(_ sender: Any) {
//}
var Str:String?
override func viewDidLoad() {
super.viewDidLoad()
let items = [Str]
let SegM = UISegmentedControl(items:items as Any as? [Any])
SegM.selectedSegmentIndex = 0
SegM.frame=CGRect(x: 70, y: 130, width: 100, height: 50)
SegM.layer.cornerRadius = 8.0
SegM.backgroundColor = .orange
SegM.tintColor = .white
self.view .addSubview(SegM)
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func Btnadd(_ sender: Any)
{
var Str = 0;Str += 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How to add and delete segments in a viewcontroller by clicking add button an delete button created in the same viewcontroller
How to insert and remove(add or delete) segments in swift4
class ViewController: UIViewController {
#IBOutlet weak var segment1: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func insert(_ sender: Any) {
segment1.insertSegment(withTitle: "\(segment1.numberOfSegments+1)", at: segment1.numberOfSegments, animated: true)
}
#IBAction func remove(_ sender: Any) {
segment1.removeSegment(at: segment1.numberOfSegments-1, animated: true)
}
}
You can insert segment by insertSegment method of UISegmentedControl and you can delete segment by removeSegment method. Let me take an example.
I create segmentController class and its UI in a storyboard.
Below is UI screenshot. In the storyboard, You can see two buttons Insert (+) and Remove (-) and UISegmentedControl. Insert button will insert segment at a specific position and Remove button will remove the segment at a specific position.
Below is code of segmentController class.
class segmentController: UIViewController {
#IBOutlet weak var segementControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func remove(_ sender: Any) {
segementControl.removeSegment(at: segementControl.numberOfSegments-1, animated: true)
}
#IBAction func insert(_ sender: Any) {
segementControl.insertSegment(withTitle: "\(segementControl.numberOfSegments+1)", at: segementControl.numberOfSegments, animated: true)
}
}
In the above code, on insert button click new segment will add at the last of segementControl. On remove button click the last segment will delete from segmentControl.
Hope it helps.
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)
}
}
}
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
I am a beginner of xcode programming. I am trying to do an action, when i click button from A view, the button of B view will be hidden. I already know i can use button.hidden = true; for self view controller but I don't know how to control button from other view.
Thanks
#IBAction func TestBut(sender: UIButton) {
setting.hidden = false
}
Before, you create a custom view with button, button action and a protocol as:
protocol CustomViewDelegate {
func buttonPressed (sender: AnyObject)
}
class CustomView: UIView {
#IBOutlet weak var button: UIButton!
var delegate: CustomViewDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
class func loadViewFromXib() -> CustomView {
return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as! CustomView
}
#IBAction func buttonPressed(sender: AnyObject) {
self.delegate.buttonPressed(sender)
}
}
In your ViewController.
class ViewController: UIViewController, CustomViewDelegate {
var firstView: CustomView?
var secondView: CustomView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstView = CustomView.loadViewFromXib()
self.secondView = CustomView.loadViewFromXib()
firstView!.frame = CGRectMake(0, 0, 100, 100)
secondView!.frame = CGRectMake(0, 200, 100, 100)
firstView!.delegate = self
secondView!.delegate = self
self.view.addSubview(firstView!)
self.view.addSubview(secondView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func buttonPressed(sender: AnyObject) {
if (sender as! UIButton) == self.firstView!.button {
self.secondView?.button.hidden = true
}else {
self.firstView?.button.hidden = true
}
}
}
the button has to be a property of the view( or view controller).
A call would look like this:
view.button.hidden = true