I need to perform some code when the user stop scrolling the picker, in other way, when the picker stop scrolling. The logic i want to follow is, once the picker stop scrolling, i get the current value and i do some database queries basing on that value.
In the picker view documentation, i don't see a delegate method that can help on such task. Any thoughts? thanx in advance.
whenever you scroll the picker view, didSelect delegate method call at the end of scroll
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected %i. ", row);
/// do it here your queries
}
try with above example and check your console
The delegate class has a method pickerView:didSelectRow:inComponent:, that you can use to detect the selected row.
Related
I want to make multiple UIPicker with following requirements -
Make Multiple UIPicker (let's call them Picker1 and Picker2)
Value of Picker2 should automatically change based upon value of Picker1 (for instance I Picker1 has countries and Piker2 should display cities in that country)
also I would like to change the data in UIView based upon the value selected from Picker1 and Picker2 (lets say we select "UK" and city as "London" then UIView should talk only about "places to visit in london"
I know it sounds like lot of asking - your help would be highly appreciated, Thanks in advance.
First, your ViewController(the one with the picker) should implement the UIPickerViewDelegate.
You should than use this method:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
It gets called every time you change a the selected row on any of the components of your picker.
For example when this gets called, you should change the UIView element to the appropriate value.
This is also the moment to reload Component2 (or whatever you want to call it) of your picker like this.
[picker reloadComponent:2];
or
[picker reloadAllComponents];
I handle clicks to UIPickerView rows with a regular method
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
but it works only for non-current rows. My problem is that I need to get taps on the already selected (current) row also. Any ideas or workarounds for didSelectRow? Thanks.
You could put a clear button over each row and handle touches from it like any other button.
How does one code this scenerio in iphone sdk?
In an expense app, when you want to add an expense, this view comes up.
After selecting "Clothing," another view slides up with a UIPickerView control that has a "done" button which will dismiss the UIPickerView. Below is a screen shot after hitting "Clothing."
I'm trying to figure out how one would slide up the UIPickerView half way up the screen with a "done" button on top of the "New Expense" view?
thank you in advance!
Use CoreAnimation and make the UIView with move from bottom to top.. and change the hidden property to true from false when required and vice versa..
Multiple UIViews can be nested as required take advantage of this to achieve what u need
You implement the UIPickerDelegate, then implement the methods that belongs to the UIPickerView.
So your interface file must contain this:
#interface YourViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
Your viewController them implements these, or more, methods:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
t´You would then instantiate the picker, set its delegate and all other properties you need it to conform to.
You could then hook up a "listener" for keeping track on when it changed.
[datePicker addTarget:self action:#selector(didChangeDate) forControlEvents:UIControlEventValueChanged];
A good place to start is the UICatalog example from the Apple developer site.
This has a lot of Picker code and a bunch of other stuff that could help getting in the mindset Apple uses for building stuff with UIElements.
Hope it helps:) it is a large subject.
This post helped me; might help others: http://sdhillon.com/animated-uipickerview
For my iPhone app, I have an editable (for delete) table view. I'd like to be able to detect that the user has clicked the "Edit" button. See this image: http://grab.by/It0
From the docs, it looked like if I implemented :
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
then I could detect it (although from the name of the method, I wouldn't think that). This proved not to work.
Any ideas on detecting this? The reason I want to is I want to hook up a "Delete all" button in the upper left hand corner when in delete mode.
thanks
It is probably not working as you expect because willBeginEditingRowAtIndexPath: is called before the editing starts.
If you want to check while in another method you need the editing property:
#property(nonatomic, getter=isEditing) BOOL editing
If you want to do something when the 'Edit' button is pressed you need to implement the setEditing method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
Which you'll find in UIViewController. (Well, that's the most likely place; there are others.)
Swift
Use below code accordingly:
open var isEditing: Bool // default is NO. setting is not animated.
open func setEditing(_ editing: Bool, animated: Bool)
When subclassing a tableviewcontroller (what most people are going to be doing most of the time since you have to override it's delegate methods just to put data into it...) you can just override the setEditing:animated: method to grab editing state changes.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(#"Editing %i", editing);
[super setEditing:editing animated:animated];
}
That passes the state change along to the super class, but lets you jump in the middle and detect the change, or alter it if you wanted...
The setEditing:animated: examples didn't work for me (on iOS 6.1) to detect the state changes that occur when you enter and exit delete confirmation mode. It seems that setEditing:animated: is only called once, when the table view goes into edit mode, but not on state changes of the cells. After some debugger fun, I arrived at a method to detect the cell state change.
My use case is different from yours. I just wanted to hide the label when the delete button is showing so that the other cell content doesn't overlap it when the Delete button slides in. (I'm using UITableViewCellStyleValue2, the one with the blue label on the left and black label on the right.)
(In your UITableViewCell subclass)
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
// showing delete button
[self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
// not showing delete button
[self.textLabel setAlpha:1.0f]; // <-- show the label
}
}
Kendall 's answer works. I did it in following way.
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
NSLog(#"Can edit %d", tableView.editing);
if (tableView.editing == 1) {
[self.editButtonItem setTitle:EDIT_BUTTON_TITLE];
}else {
[self.editButtonItem setTitle:DONE_BUTTON_TITLE];
}
return YES;
}
That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editing mode is entered, to ask each cell if it can be edited:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I don't think overriding setEditing:animated: makes sense, since you would have to subclass UITableView which is extra work and a class you need for no other reason, not to mention it would have to communicate the fact that editing had been turned on back to the controller.
One other option is to simply add the Edit button yourself - it's a built in UIBarButtonSystemItem, you can add it and then have it call your own method in which you do something specific then call setEditing:animated: on the UITableView itself.
The idea behind editing is that when editing is enabled, each cell is told to go to edit mode, and as asked if there are any specific editing controls that should be applied. So in theory there's no need to detect entry into editing mode beyond changing the appearance of cells. What are you trying to do when editing mode is entered?
I have a UIPickerView, in it's delegate I'm trying to customize the view for a row. I'm using the 3.1 SDK.
So in the delegate I have:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
// view.backgroundColor = [UIColor redColor];
return view;
}
From the apple docs:
If the previously used view (the view parameter) is adequate, return that. If you return a different view, the previously used view is released. The picker view centers the returned view in the rectangle for row.
When I run this, my UIPickerView control doesn't have any items in it, and after a short while crashes.
When I remove this particular method (which is optional for the delegate), I can see the labels I set via the titleForRow method, and it will no longer crash.
I'm pretty new to cocoa (and cocoa-touch), I'm not sure the view.backgroundColor thing will work, but even when returning the unmodified old view (which I must do anyway for most rows) crashes my app.
Am I doing something wrong?
Yes, you implement either –pickerView:titleForRow:forComponent: or –pickerView:viewForRow:forComponent:reusingView:, but not both. What is happening is that it is not calling your –pickerView:titleForRow:forComponent: because it is using your –pickerView:viewForRow:forComponent:reusingView:. You are returning the reusingView: parameter, but that is nil the first time, because there was no "previously used view" for that row.