I don't have a ton of experience with iOS development, but I feel like this should be possible. What I'm trying to do is do an IBAction within a second view controller. The IBAction is I'm trying to preform looks like this:
- (IBAction)helloWorldButton:(id)sender {
[self.textDocumentProxy insertText:#"Hello World"];
}
I've linked up the send event to touch up inside. I am able to pull up the second view controller and properly dismiss it with:
- (IBAction)dismissSecondView:(id)sender {
[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
}
That all works fine which is what's confusing me. Why does the IBAction for dismissing the view controller work but the IBAction for textDocumentProxy doesn't? I can provide more code and information if needed, but I have successfully linked up the two view controllers to appear and be dismissed. I just want a button that types "Hello World" within the secondary view controller.
Thank you for your help in advance!
I figured it out. I was thinking about it all wrong. What I was looking to do was change the label on multiple buttons, not change the entire view. What I did was this:
Take the button to change the state of other buttons, and tell it tell it to change the buttons accordingly using
- (IBAction)buttonChanger:(id)sender {
if ([_aButton.titleLabel.text isEqual: #"Hello"]) {
[UIView setAnimationsEnabled:NO];
[_anotherButton setTitle:#"World" forState:UIControlStateNormal];
[_lastButton setTitle:#"Friend!" forState:UIControlStateNormal]; //more buttons after this...
Not sure if that example makes a ton of sense (changed the variables up a bit from the original project) but that will change each button accordingly. When I push button "Button Changer", it checks the status of "aButton" to see if anything needs changing, and then changes the title of the other buttons (anotherButton, lastButton) to World and Friend! accordingly. This is exactly what I was looking to do. I realize this is a completely different route to what I was initially thinking, but it works great!
Thanks for your help!
Related
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
I'm trying to fix a problem that does not make any sense to me.
I'm applying a style to 3 of my buttons. When I click another one, there is a segue. When I dismiss the viewController, the style is not applied to my 3 previous buttons..until I click again ! I thought it was my configuration with line code that was wrong so I've deleted every customizations.
I directly configure buttons from the interface builder.
Here it my segue code even if I think it's not useful.
- (IBAction)addContact:(id)sender {
[self performSegueWithIdentifier:#"FriendsToAddFriends" sender:sender];
}
I've added some picture to show my problem :
The strangest part is when I'm back to the first page, when I click without release my click, the style comes back. It's the same for the whole 3 buttons.
I don't have any other idea to fix it. Does someone have ?
Thank you a lot in advance !
Problem solved with this code :
for (UIButton* button in buttons) {
[button.titleLabel setTextAlignment:NSTextAlignmentNatural];
}
I applied it on each following methods :
viewWillDisappear
viewWillAppear
I still don't know why this alignment change when the view will disappear.
It seems working for now.
I hope it will help someone else.
Hi I am creating an application which will switch between bluetooth and wifi mode. I want to use a UISegmentedControl to switch between the segues. The photo album I will post is an example of what I want to achieve, although that was done with .xibs.
I've already tried using the IBAction method for the segment and then using the performSegueWithIdentifier method. The latter only works one way and not vice-versa, which is what I need. Also even though it works for one way, I still get an error.
http://imgur.com/a/pTTsu
To clarify I want to be able to toggle between the two screens, but I am only able to switch from screen 1 to 2.
If you have two different segues, and you want to perform one based on what was pressed then just check which index is selected and perform that segue:
- (IBAction)yourSegmentedControlPressed:(UISegmentedControl *)sender
{
if(sender.selectedSegmentIndex==0)//left control button pressed
{
[self performSegueWithIdentifier:#"yourFirstSegue" sender:self];
}
else if(sender.selectedSegmentIndex==1)//right control button pressed
{
[self performSegueWithIdentifier:#"yourSecondSegue" sender:self];
}
}
If you want to use another button to control the segues, then simply just grab the segmentedControl outlet and check the index inside of that action. NOTE: the index will be equal to -1 if neither is selected.
I use IB and have properly wired up my buttons. I have verified they are properly wired, as some IBAction methods will have buttons hide properly. The issue I have is hiding the UIButtons when it first loads the app. In viewDidLoad I set the button property to hidden, but it doesn't hide it. Thoughts?
- (void)viewDidLoad {
stop.hidden = YES;
play.hidden = YES;
[activity startAnimating];
[super viewDidLoad];
}
After more debugging, the stop button hides but not the play.
It's hard to tell even from the code you posted why this is happening, The best guess will be that somewhere in the next lines of code you set it back to visible = YES mistakably. I would check if there is a method that shows the button that is called before it is needed.
BUT
If the initial state is hidden for your buttons. why don't you simply hide them on IB interface?
Try this
Remove outlet run the code ,test weather it works fine
Remove outlet,add hidden property via IB and run it again and test if it is seen properly.
If it works fine the problem is with code written.
Check the hidden property written via code
I am working on a tab bar application which needs a global title bar with a setttings button attached. I have achieved adding an imageview to the main window, then a label, another image, and finally a button. However, I cannot get this button to fire an action. it is set up like this:
[settingsButton addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchupInside];
Then the action defined like:
-(void)settings{
NSLog(#"please do something");
}
However nothing happens, the image doesn't even change like it is being pressed. Can i not define buttons in the app delegate this way? is it because the target is not something that is control based? i tried different targets but i must not fully understand what setting a target is. thanks for any help.
P.S.- i tried setting up the method like so as well:
-(void) settings:(id)sender{
}
and calling it by replacing the action with #selector(settings:) to no avail. Thank you.
I solved it. I forgot I changed the view behind the button to a UIImageView. I can't believe I didn't think of this before since this same issue cost me 2 hours the other day. When you have an imageView always remember to set this flag if you want stuff on it:
(someImageView).userInteractionEnabled = YES;
Hopefully I save someone an hour or two of needless headaches.
P.S. Thanks for the speedy comment !