Change to rows in two differents components in a UIPickerView - iphone

I have the following code that should change the selected rows in two different components in a UIPickerView, but only the first one changes.
[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:TRUE];
[pickerView selectRow:[pickerView selectedRowInComponent:1] inComponent:0 animated:TRUE];
But when i do the following, they both change.
[pickerView selectRow:5 inComponent:0 animated:TRUE];
[pickerView selectRow:3 inComponent:1 animated:TRUE];
What is wrong with the first code, why is it not working?

Probably because they refer to each others.
You should record the selected row and then use it:
NSInteger selectedRowInFirst = [pickerView selectedRowInComponent:0];
NSInteger selectedRowInSecond = [pickerView selectedRowInComponent:1];
[pickerView selectRow:selectedRowInFirst inComponent:1 animated:TRUE];
[pickerView selectRow:selectedRowInSecond inComponent:0 animated:TRUE];

You are overriding the value of the selected row in component 1 in the first statement with the value from component 0. Then in the second statement you are using that same value you just copied to set row selection in component 0 again.
Thus you are setting component 0 back to its own original selected value.
You need to do something like this:
int comp1OriginalRowValue = [pickerView selectedRowInComponent:1];
[pickerView selectRow:[pickerView selectedRowInComponent:0] inComponent:1 animated:TRUE];
[pickerView selectRow:comp1OriginalRowValue inComponent:0 animated:TRUE];

Related

PageController and TableView

I have a page controller and that method pageAction defined like this :
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
[self getVehicules];
[self.tableViewVehiculesPossedes release];
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
[self loadScrollViewWithPage:page];
CGRect frame = pageControlVehiculePossedee.frame;
frame.origin.x = (vosvehiculeScrollView.frame.size.width * page);
[vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Every time I change the page I need to display a tableview with some labels. Here is the code :
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[self loadScrollViewWithPage:0];
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
votreVehiculeLabel.text=#"Votre véhicule";
vehiculesPossedesArray = [[NSMutableArray alloc] initWithObjects:#"Annee modele", #"Transmission",#"Carburant", nil];
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
tableViewVehiculesPossedes=[[UITableView alloc] initWithFrame:CGRectMake(3, 80, 315, 171) style:UITableViewStyleGrouped];
tableViewVehiculesPossedes.tag=page;
tableViewVehiculesPossedes.bounces=NO;
tableViewVehiculesPossedes.backgroundColor=[UIColor clearColor];
[tableViewVehiculesPossedes setDelegate:self];
[tableViewVehiculesPossedes setDataSource:self];
[self.vosvehiculeScrollView addSubview:tableViewVehiculesPossedes];
nameVehiculeLabel.text=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele"];
self.transmissionString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"transmision"];
self.carburantString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"carburant"];
self.anneeModelString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele_annee"];
self.anneeString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"annee"];
if(page==0){
NSLog(#"0");
self.transmissionString=#"ttt";
}
else NSLog(#"1");
}
The problem is that even I put [self.tableViewVehiculesPossedes release]; on the change page method, the tableView appear overlay and the text from labels are overlay. What can I do with the tableview to make it dissapear when a new page will be display?
Please help me..I spent a lot of time with this :|
If I get it correctly I think you should keep an ivar to the table you create with the alloc in loadScrollViewWithPage, and when switching page you should removing it from the view by calling :
[mytableview removeFromSuperview];
And most likely your should take care of releasing it as well as I can't see it in your code.

UIActivityIndicatorView not working when added to UITableView

I'm trying to add a UIActivityIndicatorView to the center of my UITableView while it waits for the UITableViews data to load. I am doing this as follows:
UIActivityIndicatorView *activityIndicatorTemp = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicatorTemp setCenter:[[self tableView] center]];
[self setActivityIndicator: activityIndicatorTemp];
[activityIndicatorTemp release];
[[self tableView] addSubview:activityIndicator];
[activityIndicator startAnimating];
However the activity indicator is not showing up at all when I launch the code. I'd like to do it programmatically, any suggestions on how to fix this or why it isn't working?
please move
[activityIndicatorTemp release];
after addSubview:
[[self tableView] addSubview:activityIndicator];
The problem here is when I was setting the location of the activityindicator. I did this before the tableview had been initialized.
[activityIndicatorTemp setCenter:[[self tableView] center]];
I removed the above and it worked.

How to show the activity indicator on tap gesture

I want to show the activity indicator on tap gesture and also navigate from current viewcontroller to next viewcontroller I have written code on tap gesture method my code is -
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
It navigates to next view controller but indicator are not showing, I want when it navigate to nextviewcontroller at that time indicator also should be show for some time and then navigate to next view controller. What should I do for that?
I suspect you need to do something like this:
- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
textLabel.hidden = YES;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
[indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:indicator];
[indicator startAnimating];
[self performSelector:#selector(pushAccountAnfordern) withObject:nil afterDelay: 0.1];
}
- (void) pushAccountAnfordern;
{
AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
[self.navigationController pushViewController:accountAnfordernViewController animated:YES];
[accountAnfordernViewController release];
}
You can vary the delay. And, indeed, 0.0 may be just fine. This way of doing it lets the current run loop end, giving a chance for the spinner to start showing before starting the next bit of code. For a different approach (with potential side effects) to forcing the spinner to start before the current run loop ends this this SO question.

how to use uidatepickerview in timer?

I want to use timer with UIDatePickerView?
What I want to write code?
Pls help me.
Change mode of UIDatePickerView to timer....Do as i had done below
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,100,0,0)];
[pickerView setDatePickerMode:UIDatePickerModeCountDownTimer];
[self.view addSubview:pickerView];
[self.view setBounds:CGRectMake(0,0,320, 500)];
[pickerView release];
Good Luck!

UIPickerView without IB?

I have my pickerview set up and everything works fine, but right now I have that big pickerview taking up a lot of space in Interface Builder. It's annoying having to move it around when working in Interface Builder. How do I set the pickerview and it's location without using IB?
Try this:
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,100,0,0)];
[pickerView setDatePickerMode:UIDatePickerModeTime];
[self.view addSubview:pickerView];
[self.view showInView:self.view];
[self.view setBounds:CGRectMake(0,0,320, 500)];
[pickerView release];
The above code is for UIDatePicker, you may similarly implement it for other types of uipickers as well.