Passing data from embedded TableViewController - ios

I have a TableViewController embedded in a ViewController. My UITableView has toggles. When one of these toggles are toggled I need my number on the ViewController to increase by 1. The issue I am having is not being able to get the text to change. I have supplied a screenshot to help better illustrate.
What I have tried doing is setting a variable var colorViewController: ViewController? in the TableViewController and then trying to set the text from there using colorViewController?.displayNumber.text = screenCount when toggling a toggle. screenCount is the variable that increasing as the toggles are toggled.

How are you setting your colorViewController variable?
You need to set it to the current instance of your ViewController in your ViewController's prepare for segue.
First, you'll need to give the embed segue an identifier. Select the segue in the storyboard and give it an identifier, say ColorPickerTableViewControllerSegue.
Then handle this segue in your ViewController (this is Swift 3 code):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ColorPickerTableViewControllerSegue" {
if let colorPickerTableViewController = segue.destination as? ColorPickerTableViewController {
colorPickerTableViewController.colorViewController = self
}
}
}

Related

go back to a certain viewController

my Storyboard looks like:
User can go directly from the 1st screen to the 4th. The fourth screen contains tableView, with XIB cell design. I want user to be able to tap on a cell and get to the 3rd screen and send some data with that. I know this should be done in didSelectRowAt. But is it even possible?
Yes it is possible.
Create a segue. In your storyboard, ctrl-drag from the first button on top of your 4th screen to anywhere on your second screen. You should see a new segue created in your storyboard.
Give the segue an id. Click on the newly created segue, in the right panel, set the indentifier attribute under the Indentity Inspector tab.
Perform the segue. In your didSelectRowAt, add the following line:
self.performSegue(withIdentifier: "THE_ID_YOU_ASSIGNED_IN_STEP_2")
Send data to the destination segue. In your screen 4 view controller, add the following function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "THE_ID_YOU_ASSIGNED_IN_STEP_2" {
if let destinationVC = segue.destination as? YOUR_SCREEN_3_VC {
// Send data to your VC, such as assigning values
}
}
}

Label.text is not updating

The problem is label.text value became nil every time all the time accept in viewload. I do not know what is the issue.
I have this code in swift 3 Xcode 8, I have a label as shown below
Label to be set later
then I want to assign the label a value when the view loads as initial value
This is once view loads and it is working fine :)
later on I want to change the label value to the current Date which I would like it to be set when the user moves to another view the comes back , so I made the function open as you say ..
Here is how I call the function
function call from secondViewController
finally I want to set it to those values
last desired values
Do this in your firstviewcontroller ,define a property say :
var x : String?
Now in your secondview controller do this :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let firstviewcontroller = segue.destination as! FirstViewController
firstviewcontroller.x = "sample value"
// sample value will be displayed on the firstview controller when u move from secondviewcontroller to firstviewcontroller on the label
}
Now in firstviewcontroller set the label in the viewDidLoad() method:
dateItem?.text = x
Note: No need to create another instance of FirstViewController in prepare function because the inbuilt function of UIViewController override func prepare(for segue: UIStoryboardSegue, sender: Any?) is responsible for going from one view controller to other , you just have to compare the identifier
The problem lies in prepare for segue. When you have got your reference using segue.destination to the firstViewController, you don't need to do
s = FirstViewController(), by this you are creating a new instance and not using the one which will actually get initialised during the segue, use firstController.calcPray()

What is the correct way to use getters/setters

Salutations,
This is a follow up from my previous post (Incorrect data passed on first click of button). It was answered, but the updated edit of course occurred.
My inquiry is as follows, at the moment I have two IBActions funcs which empty, lack any sort of code, and their only purpose for existence is that they are connected to my other view controller, and as such if I remove them I have no way to differentiate between which segue should be used (My understanding is that although we can create segues between two view controllers, I do not think anything more than one would make sense (as in, how to decide which one to go with). I digress.
I tried using the following in my code before:
self.performSegue(withIdentifier: "slideTwo", sender: self);
This works well, however, it does cause double segueing. As such, I settled for the following:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let passedPic = segue.destination as! ViewControllerTwo;
if(segue.identifier == "slideOne") {
passedPic.picsChosen = sLS1;
} else {
passedPic.picsChosen = sLS2;
}
}
This works well, does what I need, but I do then have the problem of two empty IBActions:
#IBAction func slideShowOne() {
}
#IBAction func slideShowTwo() {
//self.performSegue(withIdentifier: "slideTwo", sender: self);
}
This is where I thought perhaps I could try to create a getter/setter (which swift makes far more complicated than it honestly should be). This is where I am lost.
The code looks as follows:
var picsChosen : [String] {
set(newData) {
for index in 0..<newData.count {
print("Index is: ", newData[index]);
}
self._picsChosen = newData;
} get {
return self._picsChosen;
}
}
Now, my issue is two fold. First, what is the correct way to actually access and pass values into my variable. I tried doing: myClass: ViewControllerTwo! = ViewControllerTwo();
and then use myClass.picsChosen = myArray; However, although it does appear that the data was passed successfully into my VC2, since when I loop through (inside the setter) I do see the values successfully being displayed, when I try to access picsChosen outside of that I get an index out of bounds error, in other words, the value was never associated with picsChosen. The current way I have it works well, but I want to know what is the correct way to use getters/setters, and why what I have did not work.
Cheers!
This works well, does what I need, but I do then have the problem of
two empty IBActions:
You don't need these empty #IBActions. They aren't doing anything for you. The segues are wired from the buttons in the Storyboard, so you don't need the #IBActions. If you delete the code, you also need to remove the connection from the Storyboard or your app will crash when it tries to call the removed #IBActions. To remove, control-click on the buttons in the Storyboard, and then click on the little x next to Touch Up Inside to remove the connection. Then you can delete #IBAction in your code.
if I remove them I have no way to differentiate between which segue
should be used
Not true. You have two different segues wired from two different buttons. The segue's identifiers are how you differentiate between the two segues.
I tried doing: myClass: ViewControllerTwo! = ViewControllerTwo();
When using segues, the segue creates the destination viewController for you.
This doesn't work because you are creating an entirely new ViewControllerTwo here. You are passing the values to this new instance, but this isn't the ViewControllerTwo that the Storyboard segue created for you.
When using segues, you should pass the data in prepare(for:sender:), just like you showed above. Get the destination ViewController and fill in the data you want to pass.
And now for the question you didn't ask:
How could I have done this using the #IBActions to trigger the
segue?
Remove the segues that are wired from your buttons.
Wire the segue from viewController1 to viewController2. Click on the segue arrow in the Storyboard and assign the identifier slideShow.
Assign unique tags to your two buttons. You can set this in Interface Builder. Give the first button tag 1, and the second button tag 2.
Your buttons can share the same #IBAction:
#IBAction func goToSlideShow(button: UIButton) {
self.performSegue(withIdentifier: "slideShow", sender: button)
}
In prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let passedPic = segue.destination as! ViewControllerTwo
if let button = sender as? UIButton {
if button.tag == 1 {
passedPic.picsChosen = sLS1
} else if button.tag == 2 {
passedPic.picsChosen = sLS2
}
}
}

how to Segue to uitextview from uitableview in swift and use nsattributedtext property

I am developing an IOS app using swift language in which I want to segue from uitableview's row to uitextview to show some text and I want to use attributed text property of uitextview but its not working.How to do that?
First of all, you can't segue to a UITextView; you can segue only to another view controller. But maybe you meant to say that your segue goes to a view controller with a UITextView in it. If so, then control-drag a segue from your table view cell to the other view controller, and give the segue an identifier. Then, in your table view controller, implement:
func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
if segue.identifier == "the-segue-id-that-you-put-in-your-storyboard" {
// Cast your destination view controller to the specific type
if let destinationVC = segue.destination as? <your-view-controller-type> {
destinationVC.textView.attributedText = <your-string>
}
}
}

How to identify the kind of StoryboardSegue

In my iOS project I use two kinds of UIStoryboardSegue, which present a view either within a navigation controller or as a modal view. I set the kind property in Interface Builder to:
Show (e.g. Push)
Present Modally
Now I want to be able to programmatically identify the kind of segue in order to customise the appearance of my ViewController. Like so:
class ViewController : UIViewController {
var isModal : Bool = false
...
}
class OtherViewController : ViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.destinationViewController is ViewController {
let vc = segue.destinationViewController as! ViewController
vc.isModal = TODO
}
}
}
I was hoping there would be a property, but I can't find it. I was also hoping that the segue class would differ, but I also can't find enough documentation.
I originally stumbled upon this problem trying to use the isModal in order to alternate between dismissing the ViewController vs. popping the ViewController. I have noticed that there now seems to be a better alternative, which is the UnwindSegue. However, I still need the flag in order to customise appearance..
Thanks
Maybe I'm totally wrong but can't you use the identifier of the segue?
For example name all modal view controllers with Modal<Name>. Then check
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier.hasPrefix("Modal") {
let vc = segue.destinationViewController as! ViewController
vc.isModal = TODO
}
}

Resources