Click on the addsubview event button - ios

I have a button in a subview called UIViewControllerB. Someone who taught me how to capture click event in UIViewControllerB will add a new view to UIView in UIViewcontroller A under string as my code.
This is the button in ViewControllerB displayed in UIView in UIViewController A
#IBAction func button_complate(sender: AnyObject) {
NSLog("aaa", "bbb")
}
Here is the UIViewControllerA containing UIView
#IBOutlet weak var addview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}

try code:
class UIViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func addView(/*param */) {
// do something
}
// when push to UIViewControllerB set install for viewcontrollerA
private func goToScreenB(){
let viewB = UIViewControllerB()
viewB.viewcontrollerA = self
self.navigationController?.pushViewController(viewB, animated: true)
}
}
class UIViewControllerB: UIViewController {
// In UIViewControllerB create a install of UIViewControllerA
var viewcontrollerA : UIViewControllerA?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func button_complate(sender: AnyObject) {
self.viewcontrollerA?.addView()
}
}

Related

i want to triger navigationcontroller when i press button in UIView class

I want to trigger Navigation controller to some other screen when i press the button in UIView class. How can i do this?
//Code for UIView Class in Which Button Iboutlet is created
import UIKit
protocol ButtonDelegate: class {
func buttonTapped()
}
class SlidesVC: UIView {
var delegate: ButtonDelegate?
#IBAction func onClickFinish(_ sender: UIButton) {
delegate?.buttonTapped()
}
#IBOutlet weak var imgProfile: UIImageView!
}
//ViewController Class code in Which Button Protocol will be entertained
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
var slidesVC = SlidesVC()
override func viewDidLoad() {
super.viewDidLoad()
slidesVC = SlidesVC()
// add as subview, setup constraints etc
slidesVC.delegate = self
}
extension BaseVC: ButtonDelegate {
func buttonTapped() {
self.navigationController?.pushViewController(SettingsVC.settingsVC(),
animated: true)
}
}
A more easy way is to use typealias. You have to write code in 2 places. 1. your viewClass and 2. in your View Controller.
in your SlidesView class add a typealias and define param type if you need otherwise leave it empty.
class SlidesView: UIView {
typealias OnTapInviteContact = () -> Void
var onTapinviteContact: OnTapInviteContact?
#IBAction func buttonWasTapped(_ sender: UIButton) {
if self.onTapinviteContact != nil {
self.onTapinviteContact()
}
}
}
class SwipingMenuVC: BaseVC, UIScrollViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let slidesView = SlidesView()
slidesView.onTapinviteContact = { () in
// do whatever you want to do on button tap
}
}
You can use the delegate pattern to tell the containing ViewController that the button was pressed and let it handle whatever is needed to do next, The view doesn't really need to know what happens.
A basic example:
protocol ButtonDelegate: class {
func buttonTapped()
}
class SomeView: UIView {
var delegate: ButtonDelegate?
#IBAction func buttonWasTapped(_ sender: UIButton) {
delegate?.buttonTapped()
}
}
class ViewController: UIViewController {
var someView: SomeView
override func viewDidLoad() {
someView = SomeView()
// add as subview, setup constraints etc
someView.delegate = self
}
}
extension ViewController: ButtonDelegate {
func buttonTapped() {
self.showSomeOtherViewController()
// or
let vc = NewViewController()
present(vc, animated: true)
}
}

Sending data to another controller - Without going to him

I want to send data to another controller without opening it.
Example
Main controller:
override func viewDidLoad() {
let vc = SecondViewController()
vc.test = "ABCDFER"
}
Second controller:
var test: String
override func viewDidLoad() {
print(test)
}
How to do it?
It works for me this way
class ViewController: UIViewController {
var otherViewController: OtherViewController!
override func viewDidLoad() {
super.viewDidLoad()
otherViewController = OtherViewController()
otherViewController.test = "ABCDFER"
}
#IBAction func press() {
self.show(self.otherViewController, sender: nil)
}
}
class OtherViewController: UIViewController {
var test: String!
override func viewDidLoad() {
super.viewDidLoad()
print(test)
}
}
In your Main controller, as soon as viewDidLoad() finishes your instance of SecondViewController is destroyed / deallocated. If you want to set a value inSecondViewController at that point, so you can "use" it later, you need to keep a reference to that instance:
So, in Main controller:
var secondVC: SecondViewController?
override func viewDidLoad() {
secondVC = SecondViewController()
secondVC.test = "ABCDFER"
}
Now, later - perhaps on a button tap - you want to use that same instance:
#IBAction func buttonTap(_ sender: Any) {
print("test in secondVC:", secondVC?.test)
}
Keep in mind the view life cycle, if the view viewDidLoad() it's only executed when loading the view through a xib or when view related actions are done with the controller, like addSubview().
The value is being passed and will not be deallocated while your main controller is alive.
You can force a lifecycle event to be called, but isn't recommended at all.
class ViewController: UIViewController {
var otherViewController: OtherViewController!
override func viewDidLoad() {
super.viewDidLoad()
otherViewController = OtherViewController()
otherViewController.test = "ABCDFER"
//Do not do this
otherViewController.viewDidLoad()
}
}
class OtherViewController: UIViewController {
var test: String!
override func viewDidLoad() {
super.viewDidLoad()
print(test)
}
}

Delegate doesnt work with SplitView

I have a SplitView with master(left) and detail(right) view for an iPad app. I want that, when I do click on a button in the master view, this call delegate method on parent split view and push a new view o top, but the delegate method is never called. Why?
Here the code:
Master view:
protocol newAlbumDelegate{
func openAlbum()
}
class newAlbumController: UIViewController {
var delegate:newAlbumDelegate?
#IBAction func buttonClick(sender:UIButton){
delegate?.openAlbum()
}
}
SplitView:
import UIKit
class AlbumSplitController: UISplitViewController, newAlbunDelegate {
var newAlbumToAdd:newAlbumController = newAlbumController()
//Delegate
func openAlbum(){
print("Do something")
}
override func viewDidLoad(){
super.viewDidLoad()
self.newAlbumToAdd.delegate = self
}
Any help??
Regards,
Within the masterViewController, assign the splitViewController as the delegate:
protocol newAlbumDelegate{
func openAlbum()
}
class newAlbumController: UIViewController {
var delegate:newAlbumDelegate?
#IBAction func buttonClick(sender:UIButton){
delegate?.openAlbum()
}
override func viewDidLoad(){
super.viewDidLoad()
let splitView = self.splitViewController as! AlbumSplitController
self.delegate = splitView
}
}
Your splitViewController becomes:
class AlbumSplitController: UISplitViewController, newAlbumDelegate {
//Delegate
func openAlbum(){
print("Do something")
}
}

Changing label text inside method

I'm using delegation to pass back information from a view controller.
This is the method
func writeValueBack(value: String) {
self.label.text = value
}
The function gets called and all is great apart from the label doesn't update.
The label has a value and is not returning nil, I checked with this line
println(self.label.text)
It prints the value of 'value'
So that means that the label's text is being set to 'value' but it's not updating.
I even tried using the main thread but no luck
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
I just don't know what the problem is.
Any help would be great.
Protocol:
protocol writeValueBackDelegate {
func writeValueBack(value: String)
}
EDIT:
Code for my view controller:
//
// ViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class ViewController: UIViewController, writeValueBackDelegate {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "MainSegue" {
var vc = segue.destinationViewController as SecondViewController
vc.labelText = textField.text
vc.delegate = self
}
}
func writeValueBack(value: String) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.label.text = value
})
}
#IBAction func button(sender: AnyObject) {
self.label.text = textField.text
}
#IBAction func segue(sender: AnyObject) {
performSegueWithIdentifier("MainSegue", sender: self)
}
}
Second ViewController source:
//
// SecondViewController.swift
// DelegateTesting
//
// Created by Alex Catchpole on 30/11/2014.
// Copyright (c) 2014 Alex Catchpole. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var textField: UITextField!
var labelText: String!
var delegate: writeValueBackDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
label.text = labelText
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func button(sender: AnyObject) {
label.text = textField.text
}
#IBAction func segueBack(sender: AnyObject) {
var editedText = label.text
performSegueWithIdentifier("SecondSegue", sender: self)
if (delegate != nil) {
delegate?.writeValueBack(editedText)
println("working")
}
}
}
The issue is in your second view controller, which as rdelmar pointed out creates a new instance of your first view controller instead of navigating back to the original instance.
To fix this, you could use dismissViewControllerAnimated:completion instead of performing your second segue. But an unwind segue would lead to simpler code, and can be achieved by adding this to your first view controller:
#IBAction func unwindFromSecond(unwindSegue: UIStoryboardSegue) {
if let secondViewController = unwindSegue.sourceViewController as? SecondViewController {
label.text = secondViewController.label.text
// Or whatever you need to retrieve data from the second controller
}
}
Then in your storyboard create an unwind segue from the second view controller. For example if you have a Dismiss button, control-drag from this button to the Exit icon in your second view controller scene and choosing unwindFromSecond. For detailed steps see the answer to this other question: What are Unwind segues for and how do you use them?
You can now remove the writeValueBackDelegate declaration and associated variables, the writeValueBack method and the second view controller's segueBack method

Hide a view container with a button in the ViewContainer

I have a View. In this view, I have a Container View. And in the ContainerView I have a button.
When I am touching the button of the ContainerView, I want the ContainerView become hidden.
I want to do something like that :
class ContainerView: UIViewController {
#IBAction func closeContainerViewButton(sender: AnyObject) {
//I try this : self.hidden = false
//or this : self.setVisibility(self.INVISIBLE)
}
}
Any idea how do it?
There are serval ways but here is the easiest one, not prettiest though. You should really use delegates but this is a hacky way to get started. Just create a global variable of the class that holds the container (startController in this case). Then call it from your other view controller (MyViewInsideContainer) and tell it to hide the view you´re in. I have not run this code but it should work.
var startController = StartController()
class StartController:UIViewController {
#IBOutlet var myViewInsideContainerView: UIView
....
override func viewDidLoad() {
super.viewDidLoad()
startController = self
}
func hideContainerView(){
self.myContainerView.hidden = true
}
}
class MyViewInsideContainer:UIViewController {
...
#IBAction func hideThisView(sender: AnyObject) {
startController.hideContainerView()
}
}
i think a cleaner solution is to use delegation:
in the ParentViewController
class ParentViewController: UIViewController ,ContainerDelegateProtocol
{
#IBOutlet weak var containerView: UIView!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
//check here for the right segue by name
(segue.destinationViewController as ContainerViewController).delegate = self;
}
func Close() {
containerView.hidden = true;
}
in the ContainerViewController
protocol ContainerDelegateProtocol
{
func Close()
}
class ContainerViewController: UIViewController {
var delegate:AddTaskDelegateProtocol?
#IBAction func Close(sender: AnyObject) { //connect this to the button
delegate?.CloseThisShit()
}

Resources