How to make IBAction (from button pressed on View1) finish BEFORE View2 viewDidLoad happens? - ios

I'm asking this because I haven't found answer or solution on any other topic, sorry if this question was already asked and answered. THIS didn't answer my question, although problem is similar.
I am trying to explain it as possibly good as I can.
I'm having a singleton containing data used in application, let's call it DataStore. I'm trying to change some value in it, let's say DataStore.x, using one of IBActions (by pressing one of multiple buttons in View1). This button is also responsible for changing view to View2, modal link. viewDidLoad function from View2 is using DataStore.x variable.
Problem :
DataStore.x isn't set by IBAction yet when it's being used by viewDidLoad (using NSLog gave me information that value is being set, but too late).
prepareForSegue function would be perfect if I knew inside it which button was pressed, so before I switch views I can put the proper value to DataStore.x.
Thank you for your time.
Karol

Related

In Swift, why does IBAction take the button as a parameter if its linked to only that one button?

I'm giving myself a crash course on functions and what I can't seem to understand is when using storyboard to create an IBAction by clicking and dragging from a UIButton, why does it give you the option to create a parameter for the button if the button already corresponds to the IBAction you just created? Why would creating the button as a parameter be helpful?This isn't clear to me yet but I really want to understand.
Thank you in advance.
You can connect more than one button to the same action. The button parameter then tells you which button was tapped.
Even when only connected to one button, the parameter is useful. You may wish to access the button in the action. Having the parameter gives you that direct access. No need to access any outlet property you may or may not have.

TableView didSelectRowAtIndexPath not called

So I have added a UIUITableView to a UIViewController. I can't use a UITableViewController for reasons I don't need to explain since it will be unnecessary information. Anyway, I have set the delegate, and the data source to this viewController. I've added the delegate and datasource protocols as well. The cells are populated correctly, so the datasource is working fine. I can also scroll so it all works fine.
However, I can't get the didSelectRowAtIndexPath to trigger. It SHOULD trigger, but doesn't. I've read and a lot of issues with this can be correlated to a UIGestureRecognizer, but I haven't implemented one. I also use the standard UITableView, so not a custom made one.
If I long press the cells (3-4 sec) then it gets triggered as it's supposed to. This suggests that there is some issue with another view or something absorbing the tap gesture, which I have no control over. How would I solve this?
No, it's not didDeselectRowAtIndexPath.
Yes all delegates and datasources are correct, since I can get the delegates/sources to trigger.
Yes, Single Selection is set on the TableView in the inspector.
Yes, everything has user interaction enabled.
If I just copy the code over to a UITableViewController it will work just fine, but right now that is not an option, I'm afraid. Anyone got any ideas on how to solve this? Most people who've had this issue has either had the issues in the list above, or added a UIGesture on top of the UITableView - I haven't.
I want to start by saying that I appreciate all the answers here provided, it gave me a lot of things to try out so I learned a lot - thanks! None of your suggestions worked, but simply because I'm a complete idiot. I said in the post that I did NOT implement a UIGestureRecognizerwhich I didn't...in that class, but in its super class. So I DID in fact implement it, but in a class that this ViewController was a subclass off. The only reason I didn't remember it was because I haven't looked at that super class for weeks.
Someone did suggest it in the comments that I should check for it, and I did and I was already certain I didn't implement one, so I quickly dismissed it. But now, after about 4 hours of debugging and recreating the project, adding things one by one, I eventually realized that the only thing that differed at this point was the Super Class, and the first piece of code I see when I open the file up was a GestureRecognizer...
So keep this in mind in the future everyone - I know I will. Thanks again for the help!
Sincerely,
The complete idiot.
Make sure you don't add any controller in that cell, which covers the entire cell and also the user interaction of that controller is enabled.
Due to that controller's user interaction enabled the tap action is taken by that controller and when you long press that cell, that cell will receive your tap.
Example :
UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _cellWidth, _cellHeight)];
[_view setUserInteractionEnabled:YES];
[cell.contentView addSubview:_view];
In the above case that view got your tap instated of the cell.

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

How do I link my button to the selected state?

Now, I have sort of asked this question already, but this time it's a little different. Since my last question, I have received several answers and excellent advice, and I did some extensive research as well. I am using xcode, and coding for iOS 6.
My question specifically is how do I keep my button in a temporary "selected" state when pressed? Like I want it to stay pressed through the duration of the sound and then switch back to the default image.
My button is mostly linked up through interface builder, because it's easy and less code to write. I have the sound coded in my button's IBAction to play when pressed and I have the second image that it changes to when pressed, however the Image isn't set in my code it is set in Interface Builder.
I've seen several code that seems like it would work wonders if I knew how to correctly input it. Which leads me to a second part of my question.
Can someone help me set it up?
for example:
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
This seems like it would do the trick in my head. I just don't know which variable of mine to replace "btnClear" with and setBackgroundImage isn't showing up as a command.
On my other question I received and answer in the form of a comment that made the most sense but they didn't elaborate, it was:
"you can set image for Selected state of your button, and on touchUpInside set property selected = YES.
So I have the image I want in the selected state, and my button is linked for touchUpInside, but the only "property" or action it has is to play the sound, and because my button's images are set in Interface Builder I'm not really sure how I can set selected = YES without coding a new class which sets up the image.
Now if that's what I have to do, that's fine, but I need some guidance setting that up I am highly familiar with Objective-C it's just I'm new to these default classes in the iOS SDK I'm still getting a hold of things and having to refer to tutorials every now and again.
Here's a link to my other question if it helps:
How do I make a button stay pressed?
Again, I already have everything set up regarding the Image in Interface Builder, I'm just not sure how to fit the selected state option it into my already existing button action.
Thank you!
I think what you may be looking for is a button property:
#property (nonatomic, weak) IBOutlet UIButton *button;
(Or with a more descriptive name!) Hook this up to your button in Interface Builder. Then when you want to switch to the selected image (which you said is already in IB, right?), simply do this:
self.button.selected = YES;
And to turn back to the default image:
self.button.selected = NO;

UIPickerView returning previous selection as animation isn't complete

This seems to be a common occurrence, but I am unable to work out how to resolve it. I have a 'Enter' button to utilise a selected row from a picker view; however, if the animation is not yet finished the previous value is used (event though it looks like it should be picking the closest value).
My solution is to disable the 'Enter' button while the animations are taking place.
However, it would seem that the animationKeys no longer works and I am unable to work out the starting and stopping animation delegate methods of the picker view as they are called as class methods and, for the life of me, I cannot fathom how that can link to a particular view.
Can anyone shed any light on how I can determine if the picker view is being animated or how the class methods are implemented to relate to a particular object?

Resources