Changing element in another ViewController - ios

I am new to iOS and have a very basic question. I've googled and can't find the answer.
I have a UILabel on one ViewController and view and would like to change the font color with:
myLabel.textColor = [UIColor blueColor];
but in another ViewController. Is that possible?
So myLabel is in the first ViewController and the button to change the color of myLabel is in the other ViewController.
So more information like yall asked:
My app is compiled of tons of buttons with simple commands. All the buttons are in the same area so I thought of putting some of them on a different view instead of just piling buttons on top of each other and calling .hidden = true; on them.

It is possible but it does not immediately sound like a good design. Without knowing more about your app...
If the color change is to reflect some change in your model you should effect the change through your model:
--(UIEvent)--> ViewController1 --(update)--> model --(observe)--> ViewController2 --(change color)-> textField.
If your app is not that complicated, you should at the very least implement a method on ViewController2 that is descriptive of the reason why the text field should change and send that message (call the method) from ViewController1.
You will also need to pass a reference to VC2 into VC1 - you either already have this because there is a VC1->VC2 parent child relationship or you will have to do it through a joint parent.

Related

Objective C: show textfield and textview input in label on next view?

So i've seen many questions alike mine, but all don't have the working answer, or have vague answers (and none of them marked as correct).
I have two ViewControllers:
ViewController #1 has 4 textFields to which there are pickerViews attached in order to choose an option to fill in the textfield.
Also there is 1 textView in which the user can use the keyboard.
To sketch the scenario: Label states: 'Pick a color:' User clicks on the textfield (pickerView shows up full of names of colors) user selects 'red' and the picked choice shows up in the textfield below the label: 'pick a color'.
Then when all textfields and textview are filled in, the user clicks save. In which the save button redirects the user to the 2nd ViewController.
ViewController #2:
This is where i want the input (of the textFields and textView) to be shown in the labels(if this is the correct usage). Since this is where the user will see list of the chosen answers. However i cannot manage to get this working.
What do i need to do in order to achieve this?
Also i'm still learning. Please bear with me.
Thanks in advance.
In general this site is not well-suited to "Tell me how to do XYZ" type questions. It's intended for getting help with code you have already written. You're new, though, so I'll take pity on you.
You should post the code that you have, showing how you get from view controller 1 to view controller 2.
I'm going to assume that you have a segue from view controller 1 to view controller 2.
(You should probably have code that disables your send button until all 4 text fields are filled in with valid colors.)
Anyway, let's assume that your send button triggers a segue, either directly, or in an IBAction that triggers a segue through a call to performSegueWithIdentifier.
You need to define properties for your 4 color strings in view controller 2.
Then, add a prepareForSegue method to view controller 1 if you don't have one already. In prepareForSegue, make sure the segue matches the segue identifier that invokes view controller 2 (good idea even if you only have 1 segue now, since you might add more segues later.)
ViewController2 *vc2 = (ViewController2 *) segue.destinationViewController;
Assuming it's the right segue, get a pointer to the destination view controller and cast it to the type of ViewController2 so you can refer to your color properties.
Then use code like this to pass the colors to view controller 2:
vc2.color1 = myColor1TextField.text;
vc2.color2 = myColor2TextField.text;
vc2.color3 = myColor3TextField.text;
vc2.color4 = myColor4TextField.text;

Replace views in detail part of the screen after user's action

I am new to Swift and app development. I have a design question. I am trying to make a view that contains a slider, but that as soon as the "touch up inside" action is performed, is replaced by a progress bar + button. If the button is pressed, then we go back to showing only the slider. This view will be not take the whole screen, only part of it.
What would be the best way of doing this? I have already investigated several options:
1. using a navigation controller with a segue triggered by the slider that goes into a new scene with a progress bar & button.
2. creating a custom view with two properties: a slider and a custom view (progress bar & button). The slider can be laid out using interface builder, and the custom view can be loaded from a nib file when needed.
3. creating a custom view with two properties: a slider and a custom view (progress bar & button). The new progress bar and button are created programmatically whenever the action is triggered on the slider.
I have already tried options 1 and 2 to some extent with no success. Since I am a beginner, I am trying to use the IB as much as possible. What is the best option (if any) from the list?
You can do this directly on the Storyboard without needing to create a custom view class, but you'll need a few lines of code in any case. Just drag a Slider into your View, and then drag a button and a progress view directly on top of that. Now select the button, and in the Attributes inspector, tick the box next to "Hidden". Do the same with the progress bar. Then just open the assistant editor and connect references to all 3 of those. You'll also need to create an action for the button (I've called it change), and make sure you leave the type field as AnyObject. Add the following line inside ViewDidLoad:
slider.addTarget(self, action: Selector("change:"), forControlEvents: .TouchUpInside)
This line just makes it so that change gets called anytime the user uses the slider. Obviously change slider to whatever you name your UISlider. You can implement the change function like this:
#IBAction func change(sender: AnyObject) {
slider.hidden = !slider.hidden
button.hidden = !button.hidden
progressBar.hidden = !progressBar.hidden
}
This is a simple implementation that just toggles between true and false for each of the items, but you'll probably want to do it differently depending on what this project does.
Now, if you want to put this functionality in multiple places in your app it might be easiest to create a custom view using the same concept as above, in which case check out this tutorial on how to create an IBDesignable UIView.

implementing textViewDidChange:

So, I have this very simple list project with objective-c, where every item on the list should have a title and a description. When clicking on one of the items, the detail view of the item shows up. The title, on the navigation bar is the item's title, and the rest of the view is the item's description. By clicking on the title, the user should be able to change it, and by clicking on the description they should also be able to change it.
Up until changing the title everything is going OK, I've managed to do that. The problem is when it comes to changing the description. I tried using the delegate, but I guess I must be using some command wrong, because it's simply not working. I tried looking for it online, but all I find are solutions for the TextField, which doesn't really help me, since I want to display a possibly multiline description, and I would rather not have to change from a TextView to a TextField every time the user wants to edit it. So if anyone could help me out with that, that would be awesome.
Here is the code where I try using the delegate.
on viewDidLoad I have this:
[self.descriptionLabel setDelegate:self.descriptionLabel.delegate];
and then I have this, where it should perform the actions after the text is changed:
- (void)textViewDidChange:(UITextView *)textView {
NSLog(#"Hello World");
}
P.S.: I was also looking for a way to have the "Done" button show up over the keyboard, because as of now, once the keyboard is brought up, the only way to get it down again is going back to the table view and then into the detail view again. I tried a couple of things already, but I'll admit that I wasn't too thorough on my research. So, as a side note, if you could show that too, it would be great, otherwise, the one thing I'm really after here is the question above. Thanks for any help! :]
Summary of answer based on the comments.
The line:
[self.descriptionLabel setDelegate:self.descriptionLabel.delegate];
needs to be:
self.descriptionLabel.delegate = self;
You want the view controller to be the delegate since the view controller implements the text view delegate methods.
You also need to indicate that the view controller conforms to the UITextViewDelegate protocol.
In your .m file, add the following:
#interface DetailViewController () <UITextViewDelegate>
#end

Changing UIToolBar (appearance) at runtime

I want to change an UIToolBar at runtime. In it's initial state, is has only one button, when that button is pressed i want it to change it's appearance to show 4 buttons. One of these buttons should cause the first UIToolBar to reappear.
Im seeing two approaches:
1) Have two UIToolBar nibs, and load them as needed.
2) Having all buttons on the first UIToolbar, and hide/show them as needed.
What would be the correct approach?
Personally, I would want to see all 4 button at initial launch with only relevant button in enabled state and rest in disabled state. Once I tap on the already enabled button I should see other buttons getting enabled. This is less surprising UI for end user. However, you can also go with #2 mentioned above in which case you might want to add some animation effect for better user experience.
The second approach would be better, because if you want to add more buttons tomorrow, you need to maintain 2 nib files instead of one.
But, think again is creating toolbar in xib file good solution?
I would create custom toolbar extending UIToolbar class and make 2 methods in it:
-(NSArray*) toolbarButtonsInitial;
-(NSArray*) toolbarButtonsExtended;
-toolbarButtonsInitial method returns UIBarButtonItems for initial state
-toolbarButtonsExtended method returns UIBarButtonItems for second state.
IMHO, this way has several advantages:
Your xib file doesn't have hidden buttons, or some button above other
one
If you need to add or remove some buttons you can do that easily for
each state
You can easily reuse this toolbar on other screens and create new
states if necessary

Suppress darkening of main view when UIActionSheet displays

I have a UIActionSheet and when it displays it darkens the main view behind it, I am under the impression this is default behavior for an action sheet. My question is, how do I override this behavior to leave the main screen the same tint as it normally is? I'm assuming I do something like:
mainView.alpha = 1f;
Or something...I can't remember if alpha needs to be maxed or 0 to leave a screen with the same coloration/transparency. Anyways, if that is correct where should it happen? My action sheet is being called in place of a keyboard for a UITextField.
Thanks in advance.
You can't change this behavior. UIActionSheet is actually presenting a whole new view overtop of your view. It's this additional view that adds the tint. There is no API to change this.
Your best solution is to implement your own custom equivalent to UIActionSheet so it does exactly what you want.

Resources