How to call pickerView: didSelectRow: inComponent: without user interaction? - objective-c

I have a 2 components, dependent pickerView. I have a preview UIImageView that changes each time the user uses the pickerView (and triggers the didSelectRow:inComponent:). This works just as I expected.
However, I want the pickerView to trigger didSelectRow: when the app launches, so that there is a Preview the moment the user sees the UIImageView. In viewDidLoad, when I try this:
[self pickerView:picker didSelectRow:row inComponent:component]; // row and component have 0 values.
next thing that happens - the pickerView shows up blank! Remove this line, and it works perfectly again. I tried this as well:
[picker selectRow: row inComponent:component animated:NO];
thinking that it will trigger didSelectRow: however it doesn't trigger it and my UIImageView remain blank.
Any advice on how to call the didSelectRow: method without the users help?
Update: by the way, the picker is inside another UIView (not the main one). In case it matters.

didSelectRow:... will only be called if a row has been selected by user interaction. If you select a row programmatically with selectRow:... then you have to add you own logic to update other components like the image view.
The principle is valid for many other delegate functions, such as e.g. tableView:didSelectRowAtIndexPath:.

Based on your comments, this what you are looking for then.
Does UIPickerView's selectRow:inComponent:animated: call pickerView:didSelectRow:inComponent:?
You have to manually call the didSelectRow method.

Related

Table View Checkmark if Task Complete

I'm working in Objective C. I have a UITableViewController with about 25 cells that push to a UIViewController. When the user hits back, I want to see if the user entered the correct data for the given cell. (I have a working bool , we'll call it isCellComplete for now). If isCellComplete is true, I want to add a checkmark as the accessory to the cell. I've been trying to put the test in cellForRowAtIndexPath but that method does not seem to run and refresh the cells everytime the view appears. Anyone have suggestions?
You should look into the UITableView method reloadRowsAtIndexPaths:withRowAnimation:. This is much more elegant than reloading the whole table view. And if you don't want an animation, you can specify UITableViewRowAnimationNone and it will look just like reloadData but be much more efficient.
You should do the check in tableView:cellForRowAtIndexPath: and if the check passes, set the cell's accessory to UITableViewCellAccessoryCheckmark. Then when you tell the table view to reload the appropriate row(s), it'll automatically call tableView:cellForRowAtIndexPath: on the data source and update that cell.
You could just call
[self.tableView reloadData];
in
-(void) viewWillAppear:(BOOL)animated
And that will make cellForRowAtIndexPath be called again when the view appears

How to enter delete confirmation state UITableViewCell from custom action?

I have a problem, I want to show right delete confirmation button of my UITableView's cell. Swipe to delete works just fine but I also want this to happen without swiping.
I would like to change cell's state from default to showing delete confirmation. There is bool property showingDeleteConfirmation but it's read only.
[self setEditing:YES animated:YES];
From custom cell's class doesn't do anything.
How to change cell's state to showing delete confirmation without swipe?
i.e. after swipe gesture on a cell is recognized what is being called? I would like to call it manually.
That is a delegate method meaning you should not call it yourself as that is a part of the process handles by the tableView class.
Instead you'll want to override the call class, prepare the method to show your button (or whatever you want to show) and animate other views trimming their frames. Doing that you'll understand the difference between the delegate and instance messages as you'll probably need to notify the tableView or it's delegate that your method is complete, the button is shown and other views are also animated.

Timing of Passing Variable between Two UIViewControllers

Not sure how to ask this question so it will makes sense, but let me try. My app opens a UIViewController, and then calls another one. When it opens the other one it places a variable in a textfield successfully. When the user closes the 2nd UIViewController and returns to the 1st UIViewController, I pass back the variable and place it in a textfield. This is all successful at this point. The variable is being passed back and forth with no problems.
So here is what I am trying to do: upon returning to the 1st UIViewController I run a query statement which uses the variable in the textfield as a key to pull a record from a SQLite table. I use the NSLog to check the code and I see that the textfield is empty, but when the UIViewController appears the variable is in the textfield.
Are with me so far? I hope so…
I am running the query in the ViewDidLoad. I am thinking that the ViewDidLoad is running before it copies the variable from the 2nd into the textfield.
My main question is: should I be running my query statement in the ViewDidLoad or some please else to get the variable in the textfield. Basically, all I want to do is pull a record based on the value in the textfield upon returning from another UIViewController.
Thank You!
Technically, your viewDidLoad won't be called if your original view was never dealloced. The delegate methods that will be called are viewWillAppear: and viewDidAppear:.
You can pass the value from the second viewController back to the first one with a custom made delegate method. What you should keep in mind is that you should do the query with the NSString you pass back with the delegate method, and place it in the textField during viewWillAppear:.
Hope this helps!
viewDidLoad is only called when the view controller is loaded up from a nib. If you are switching back to one view controller from another, it won't be executed again. Try putting the code in viewWillAppear.

tabBarController and first Tab viewDidLoad

I have created a tabBarNavigated Application. In second tab, I do something that works fine, but now I want to do something in the first Tab, so first I try to NSLog a string, but I get no reaction.
- (void)viewDidLoad
{
NSLog(#"Test");
}
If I add a label to the view, it will be displayed, but no reaction on my code.
if I start my app, i see this view, but i can't call any actions in this method, even if i change the tab, and go back to the first one, still no logs.
I try to NSlog in GehaltView
this is the mainWindow
viewWillAppear dosn't work :(
The -viewDidLoad method is only called when your view is loaded. This method will not be called again unless the view gets unloaded, in which case -viewDidUnload will be called. A view can be unloaded if there is a memory issue, but otherwise they generally stick around.
If you want to trigger an action that happens every time the view appears, then you can use the -viewWillAppear: method instead. This method is called every time the view re-appears. You can track when the view disappears with -viewWillDisappear, and watch the two get called as you toggle between the two tabs.
Note also that -viewDidLoad may get called before the view appears, but -viewWillAppear will only be called when the view actually appears (or moments before, as the will indicates).
EDIT: The code should read
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"View Will Appear");
}
EDIT: This entire answer assumes that you have a subclass of UIViewController. It seems to me that you are by-passing using viewControllers, which in general is a bad idea.
I found the solution, in interface builder i have to add a custom class to the forst tab.

IPad dismiss Keyboard without knowing which Textfield opened it

Is there a way to do a general resignFirstResponder to hide the keyboard regardless of what textfield/view/etc calls it?
Reason is I have a lot of textfields on my view and don't want to have to resignFirstResponder for all textfields to hide the keyboard. Just want a general
[self resignFirstResponder].
Any ideas?
Thanks in advance
I know that this has already been marked as answered, but for those that run into this like I did you can just use the following method on the view that contains the textfields.
- (BOOL)endEditing:(BOOL)force
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign. UIView Documentation
[self.view endEditing:YES];
it will hide keyboard when we click on view.
You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
this will dismiss the keyboard globally without the reference.
hope this will help you.
The easiest way to do this is to have a method for whenever you want to dismiss the keyboard that looks like this:
-(void)dismissKeyboard {
[firstField becomeFirstResponder];
[firstField resignFirstResponder];
}
You can check these questions:
Is it possible to make the iPhone keyboard invisible / remove it without resigning first responder?
Hide Input Keyboard on iPhone Without Knowing First Responder?
In summary:
You can call becomeFirstResponder on some other thing that you choose. It could be a UIViewController or a UIView. I had a similar problem before, I needed to make my keyboard go away when I was pushing my view controller back to its caller, without knowing which textfield was the first responder. Then, on viewWillAppear of my view controller which I was returning back, I called [self becomeFirstResponder] and the keyboard of the pushed view was gone. Because this made whichever text field was it loose being the first responder
In my own app when I had more than one text field and would like to make the keyboard go away regardless which of the fields called it, I would just wrote a method and let each and every of them resignFirstResponder.
I assume that as a programmer, you should have the clear knowledge how many text fields are on your view controller and how you can access them, otherwise it'll get messed up and you app won't look good... :-P