Anyone knows how to make continuous values in a component of a custom picker, like months wheel in date UIPickerView? Here is my source array data:
self.one =[[NSMutableArray alloc]init];
for (int i=0; i<=101; i++) {
[one addObject:[NSNumber numberWithInt:i]];
}
//and here my titleforrow method
if (component==0) {
return [[one objectAtIndex:row] stringValue];
}
Take a look at DialController here.
There's a video and some example code.
You might be interested in this DLPickerView. This picker view can totally replace UIPickerView. And yes, you can make DLPickerView scroll cyclically. And config many other new features that UIPickerView doesn't have.
Related
I am using buttons and I assigned tag 0 to 10 . Then I made an action to get the clicked button's tag, and now I want to display the tag in a label . Also I have a cancel button C. If user wants to delete any number, he can click C button that I want to remove number from the label .
This is my screenshot to touch the number
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Ezywire";
addnum=[[NSMutableArray alloc]init];
numbber=[[NSString alloc]init];
}
- (IBAction)NumberAction:(id)sender {
NSInteger tagvalue = [sender tag];
NSString *current=[NSString stringWithFormat:#"%ld", (long)tagvalue];
[addnum addObject:current];
NSString *temp;
for ( int i=0; i<[addnum count]; i++) {
numbber=[numbber stringByAppendingString:[addnum objectAtIndex:i]];
}
NSLog(#"data===%#",numbber);
ValueLable.text= numbber;
}
But in the label I am getting repeated number like this. How to implement this.
For example if user enters 2 then in the label
2
then he enters 7 then in the label
27
then he entered 9 then in the label
279
........ like this .
If user clicks C, then it remove from label last value is (last value removed)
27
The problem in your code is that numbber is initialized when the view is loaded, and never gets cleared again. However, each time a button is pressed, the whole addnum of digits gets appended to num again, creating repeated digits.
Fix this by removing num as an instance variable, making it a local to NumberAction: method, and setting it to an empty string every time the number is pressed.
Since you are planning to support the clearing action as well, you should make a private method that combines the digits from addnum array into a string. This way your NumberAction: and ClearAction would share the code that formats the array and sets the label. Your NumberAction: method would append a number and call FormatAndSetLabel, while the ClearAction method would remove the last digit if it is available, and call FormatAndSetLabel as well:
- (IBAction)NumberAction:(id)sender {
NSInteger tagvalue = [sender tag];
NSString *current=[NSString stringWithFormat:#"%ld", (long)tagvalue];
[addnum addObject:current];
[self FormatAndSetLabel];
}
- (IBAction)ClearAction:(id)sender {
if (!addnum.count) return;
[addnum removeLastObject];
[self FormatAndSetLabel];
}
-(void)FormatAndSetLabel {
NSMutableString *temp = [NSMutableString string];
for ( int i=0; i<[addnum count]; i++) {
[temp appendString:addnum[i]];
}
ValueLable.text= temp;
}
Also it might be interesting for you to have a look at Paul's Hegarty Stanford iOS development course (iPad and iPhone Application Development, Fall 2011)
https://itunes.apple.com/ru/itunes-u/ipad-iphone-application-development/id473757255?mt=10
Calculator app is used here as an example. Must see for the beginners.
Sorry to kind of be vague in the question. I currently have a pickerView populated by an array of strings. The string is set by this function...
_homePlayer = _homePlayersArray[indexPath.row];
// add to the copy
[_homeConfirmedPlayersArray addObject:[NSString stringWithFormat:#"%d %# %#",_homePlayer.number,_homePlayer.firstName,_homePlayer.lastName]];
I have it populate the pickerView which works properly. I now want to be able to pick from the picker and take just the first part of the string and set it as an NSString. Is this possible and if so how would i go about doing this? Could i change some things to be able to accomplish this? thanks in advance!
In the delegate for your UIPickerView
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
Player *player = _homePlayersArray[row];
NSInteger number = player.number;
}
I have a UIPickerView inside a custom tableViewCell (subclassed). I'am able to populate it and get data back from it. More or less.
I have this method I implement in order to get info everytime some of the components changed:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (self.pickerDelegate !=nil &&[self.pickerDelegate conformsToProtocol:#protocol(PickerCellDelegate)]) {
if ([self.pickerDelegate respondsToSelector:#selector(somethingOnThePickerIsSelected:selectionArray:)]){
NSMutableArray *pepe;
for (int i=0; i<[[self.cellPickerInputDictionary objectForKey:#"components"] count]; i++) {
NSObject*foo=[[[self.cellPickerInputDictionary objectForKey:#"components"] objectAtIndex:i] objectAtIndex:[self.cellPicker selectedRowInComponent:i]];
[pepe addObject:foo];
NSLog (#"foo: %#", foo);
}
NSLog (#"pepe: %#", pepe);
[self.pickerDelegate somethingOnThePickerIsSelected:self selectionArray:pepe];
}
}
}
I have two components, but in order to make it "universal" (independent of a particular situation) I don't want to hard-write numbers here and there.
In the example shown, I don't understand why the NSLog shows correct for the variable foo but shows null for the NSMutableArray pepe.
Any ideas? Thanks in advance.
You are not allocating your mutable array pepe.
NSMutableArray *pepe=[[NSMutableArray alloc]init];
In fact, you can do that on your init method, and declare pepe as property
Currently I have a custom view table cell and a text field just above it. I want to get the text from the UItextfield and put that into an NSMutableArray.
Pseudocode:
String text = _textfield.text;
[array addObject:text];
NSLog{array}
In my header file I have created the textfield and the array.
I currently receive the error : 'CustomTableView:[340:11303] array: (null)' when I NSLog.
I am not to sure why the text from the textfield is not getting added to the array. If any one is able to help it will be greatly appreciated.
Note - My textfield is above the custom cell not in it. I have even tried just adding a string to the array directly and logging it and I get the same error. So I would assume that this is something to do with the array.
did you initialize your Array.take a MutableArray and initialize it.
NSMutableArray *array=[NSMutableArray alloc]init];
You mentioned that you have declared the textfield and the array in your header file...
Have you initialised the variable array?
e.g.
array = [NSMutableArray new];
It looks like you are not actually creating the array. In Objective C, you do not create things in header file, you declare them. The implementation files(.m files) do all the work.
Try this:
NSString *text = _textfield.text;
array = #[text]
NSLog( #"%#", array );
This is how you should print your array,
NSLog(#"%#", array);
It looks as if your a newbie to ios.Go through the objective-c and Read the apple documentation carefully.
NSString * text = self.textfield.text;
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:text];
NSLog(#"%#",array);
For me this is what worked...
I have taken one textfield inside tableviewcell. I am creating textfields based on dynamic data. My requirement is , I need to get textfields text which are created dynamically.
For getting text in another method
NSIndexPath *indexPath = [tableViewObj indexPathForCell:customCell];
if (indexPath.row==0)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==1)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
else if(indexPath.row==2)
{
[arrayPhoneNumbers addObject:customCell.textFieldObj.text];
NSLog(#"array is :%#",arrayPhoneNumbers);
}
Like this I have added textfield text to array. Let me know if you have any doubts.
I'm loading a TableView from Core Data and it works like a charm. The data contains two fields: Category and Distance. The initial load of the table uses an array with the objects sorted based on Distance. I have a button in the Navigation Bar that I want the user to use to toggle between a Distance-sorted view (the default) and a Category-sorted view. My code for the toggle is:
-(void)toggleView {
NSString *baseItem = #"Proximity View";
NSString *currTitle = self.title;
NSComparisonResult result;
result = [baseItem compare:currTitle];
if (result == 0) {
self.title = NSLocalizedString(#"Category View",#"Categories");
tpData = tpDataCat; //tpDataCat is an array sorted by Category
[self.tblView reloadData];
} else {
self.title = NSLocalizedString(#"Proximity View",#"Distances");
tpData = tpDataDist; //tpDataDist is an array sorted by Distance
[self.tblView reloadData];
}
[baseItem release];
[currTitle release];
}
When I click the toggle button and fire `toggleView, the app just crashes. Any help would be greatly appreciated!!
You shouldn't be releasing baseItem and currTitle.
I would recommend reading the Memory Management Programming Guide; it's an excellent document that can provide background on the appropriate ownership of objects and when releasing would be required.