I'm currently working on my first "big" project. The project is a simpel converter for different currencies. I wonder if it is possible to in some way invert the two arrays. I don't know how I should implement the if statement. Becouse if i put it in the title for row method i'm will probably get the control may reach non-void error message. I may explain this pretty bad but i just started programming.
if(!reverse) { // do nothing } else {
// swap the two pickerviews
}
//Toggle reverse on/off <->
- (IBAction)Reverse:(id)sender {
reverse = (reverse+1)%2;
NSLog(#"%d",reverse);
}
//what title for row
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [Datarray objectAtIndex:row];
}
return [Datarray2 objectAtIndex:row];
}
I see what you're trying to do. Instead of having two or multiple PickerViews. Just change the DataSource and reload the PickerView. if you only have two currencies just use a flag, otherwise create a NS_ENUM to identify which array the PickerView DataSource should use and in the DataSource methods like, pickerView:titleForRow:forComponent: use a switch case of if-else statements to identify the correct dataSource array.
// .h file
typedef NS_ENUM (NSInteger, CURRENCY_TYPE) {
CURRENCY_TYPE_US,
CURRENCY_TYPE_DK
};
#property (nonatomic, strong) NSArray *dataArrayUS; // In this example, these arrays contains NSStrings
#property (nonatomic, strong) NSArray *dataArrayDK;
#property (nonatomic) CURRENCY_TYPE currencyType;
// .m file
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (self.currencyType) {
case CURRENCY_TYPE_US:
return self.dataArrayUS[row];
break;
case CURRENCY_TYPE_DK:
return self.dataArrayDK[row];
break;
}
}
- (IBAction)currencyChangingAction:(id)sender
{
// Add some logic that changes to the self.currencyType to the correct currency.
self.currencyType = CURRENCY_TYPE_US; // or _DK
// And Reload Picker
}
EDITED:
Is this what you want? - This will only work if you have two Arrays though.
//what title for row
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return (component == 0) ? [Datarray objectAtIndex:row] : [Datarray2 objectAtIndex:row];
}
Related
I have put a UIPickerView for the user to select how they are feeling; annoyed, happy, sad etc. However I cannot convert what they have selected to a string or text. Is it possible to do this? If not how can I save the row/option they have selected in the picker?
If i understood your question correctly, here's what is needed. Check out the project at this gitHub commit or download zip.
Using typedef enum: create a enumeration that holds all values of emotions; happy, okay, neutral, sad, unhappy, etc. All with unique values assigned to each one like so:
Header File
//Number Values for each value of emotion. Add new emotions to the bottom of list, sort will not
//be effected in this declartion
typedef enum : NSUInteger {
CTEmotionNone = 0,
CTEmotionHappy = 1,
CTEmotionOkay = 2,
CTEmotionNeutral = 3,
CTEmotionSad = 4,
CTEmotionUnhappy = 5,
CTEmotionVeryHappy = 6
} CDEmotions;
Translating Number Values to text: There's different ways to doing this part but i liked this. Create an inline method, or function, that will return a string for the corresponding value of emotion:
Header File
//Here is where the title for each number value will be transalated
static inline NSString * NSEmotionTitleForEmotion( CDEmotions emotion) {
switch (emotion) {
case CTEmotionNone:
return #"Empty"; break;
case CTEmotionVeryHappy:
return #"Very Happy"; break;
case CTEmotionHappy:
return #"Happy"; break;
case CTEmotionOkay:
return #"Okay"; break;
case CTEmotionNeutral:
return #"Neutral"; break;
case CTEmotionSad:
return #"Sad"; break;
case CTEmotionUnhappy:
return #"Unhappy"; break;
}
}
Now the Order: creating another inline method, but this time returning an array with the order you want to display the emotions:
Header File
//Sorted int values in this array. Sort will be shown here. Do not put text in here, because then
//to know what "Very Happy" is you'll ahve to compare it to a string vs just saying 1 == 1, that
//means CTEmotionHappy
static inline NSArray * NSEmotionsList() {
return [NSArray arrayWithObjects:
#(CTEmotionNone),
#(CTEmotionVeryHappy),
#(CTEmotionHappy),
#(CTEmotionOkay),
#(CTEmotionNeutral),
#(CTEmotionSad),
#(CTEmotionUnhappy), nil];
}
Now using these functions will be fun :) I'll do it in your case using - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component from the UIPickerViewDataSource:
ViewController.h
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [NSEmotionsList() count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return NSEmotionTitleForEmotion( [[NSEmotionsList() objectAtIndex: row] intValue]);
}
And lastly implement the delegate method - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[labelEmotion setText: NSEmotionTitleForEmotion( [[NSEmotionsList() objectAtIndex: row] intValue])];
}
As beginner in iOS Developing I struggle with specific implementation ofUIPickerView.
I already know how to createUIPickerView with specific data.
My goal is a bit complicated though - I would like this PickerView to print me selected value. For example - like in simple currency calculator - I pick EUR currency and I have value "4.30" printed out.
FYI - I need to pass this value to another ViewController just to be multiplied.
Any help will be appreciated!
Here's sample code - for now I am still trying to understand what should I do here
#interface SecondViewController ()
{
NSArray *currency;
}
#property NSArray *currency;
#end
#implementation SecondViewController
#synthesize currencyValue;
#synthesize currency;
- (IBAction)Back:(id)sender {
[self dismissViewControllerAnimated:NO completion:NULL];
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.currency count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:
(NSInteger)component {
return self.currency[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[currencyValue setText:[NSString stringWithFormat:#"Exchange rate of currency : %#",[currency objectAtIndex:row]]];
}
-(IBAction)currencyValue:(id)sender {
}
- (void)viewDidLoad {
[super viewDidLoad];
self.currency = #[#"EUR", #"PLN", #"CFH", #"DKK", #"SEK", #"NOK"];
In UIPickerViewDelegate you can set the rows' titles with pickerView:titleForRow:forComponent: as described in the documentation.
The UIPickerView's selectedRowInComponent: will return you the current selected row: use the previous data source with this index to retrieve the current selected title.
I'm looking for a spinner control something similar to this for ios:
http://github.hubspot.com/odometer/docs/welcome/
can't find anything so far, please share if you know any.
there is one third party library , by which you can do this https://github.com/Rizh/RCounter
https://github.com/naked-apps/NACounter
https://github.com/jonathantribouharet/JTNumberScrollAnimatedView
The nearest thing in iOS, similar to this is UIPickerView. You can create a UIPickerView in the Interface Builder or through code.
Next, make your class conform to UIPickerViewDataSource and UIPickerViewDelegate.
In your viewDidLoad: method, assign the delegate and dataSource of the UIPickerView to the respective class.
And finally add the following methods to your class.
// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 5;
}
// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%ul",row];
}
I have an app where the user scans a QR code into an NSString, which I then need to put into a UIPickerView.
What would I use to set the text of the UIPickerView row as the NSString from the QR code?
Firstly, you need to have an array of strings and implement the UIPickerViewDataSource and UIPickerViewDelegate in your .h file. After this, you will have to make use of the delegate methods of UIPickerView like as shown :
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//return number.
}
This will return the number of sections that you want in your pickerView.
(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return array.count;
}
The above method will return the number of rows in particular component. If you have an array, then probably it will be equal to the number of elements in your array. And finally this last one.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str;
if(pickerView == yourPickerName)
{
str = [array objectAtIndex:row];
}
return str;
}
This method will return the text value for each row in your picker. And don't forget to connect your pickerView's datasource and delegate in your Storyboard.
Hope this helps.
Okay, I need a picker so that a user can select from a list of predefined options. Can someone give me the easy, simplified version of how to populate a picker view from an array of NSStrings? Then, how do I read that value from the picker? I'm noticing that things like nameOfPicker.value and nameOfPicker.text do not work here.
Thank you!
I have already read the following documents and I don't really understand what they are getting at.
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UIPickerView.html
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11
It is quite similar to how you populate data in a UITableView, by setting datasource and delegate.
1. First step is to set the delegate of the picker view. In .m file you set your datasource and delegate
#interface YourViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
{
}
#property (strong, nonatomic) UIPickerView *yourPickerView;
2. Assign the datasource and delegate
self.yourPickerView.delegate = self;
self.yourPickerView.datasource = self;
3. Implement datasource and delegate
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.yourArrayofStrings.count;
}
//The title that should be shown in picker view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *yourTitle = [self.yourArrayofStrings objectAtIndex:row]
return yourTitle;
}
//This is called when Picker view value is selected
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog #(#"Selected row = %#",[self.yourArrayofStrings objectAtIndex:row]);
}