How to load a picker view by a for loop? - ios

I want load my picker by a for loop.From 1 to 999.I loaded manually.
My code is here.How I use a "for loop" for load automaticaly. Thanks
- (void)numberWasSelected:(NSNumber *)selectedIndex element:(id)element;
#synthesize numbers = _numbers;
#synthesize selectedIndex = _selectedIndex;
self.numbers = [NSArray arrayWithObjects:#"1", #"2", #"3",............#"999" nil];
- (IBAction)selectNumbers:(UIControl *)sender {
[ActionSheetStringPicker showPickerWithTitle:#"Select a number !" rows:self.numbers
initialSelection:self.selectedIndex target:self
successAction:#selector(numberWasSelected:element:)
cancelAction:#selector(actionPickerCancelled:) origin:sender];
}
- (void)numberWasSelected:(NSNumber *)selectedIndex element:(id)element {
self.selectedIndex = [selectedIndex intValue];
self.numberTextField.text = [self.numbers objectAtIndex:self.selectedIndex];
}

NSMutableArray* array = [NSMutableArray new];
for (int i = 1; i <= 999; i++) {
[array addObject:[NSString stringWithFormat:#"%d", i]];
}
self.numbers = array;

Related

IOS/Objective-C: NSArray randomization issue

I have this array with strings and I want to randomize it and display the output in a label. But with this code I'm having always the same strange output which is "2"...Any ideas what I'm doing wrong?
- (void)viewDidLoad {
[super viewDidLoad];
self.quoteInput.delegate=self;
self.quoteLabel=_quotationLabel;
self.correctLabel.alpha=0;
self.wrongLabel.alpha=0;
_quoteInput.delegate=self;
_levelOne = #[
[words Quotes:#"First Quote."],
[words Quotes:#"Second Quote."],
[words Quotes:#"And so on."]];
}
-(void)randomQuotes{
for (NSInteger x = 0; x < [_levelOne count]; x++) {
NSInteger randInt = arc4random() % ([_levelOne count] - x) + x;
[_levelOne objectAtIndex:randInt];
NSString *one = [NSString stringWithFormat:#"%d", randInt];
self.quoteLabel.text = one;
}
}
- (IBAction)generateQuote:(UIButton *)sender {
[self randomQuotes];
}
EDIT
The words class:
+ (instancetype)Quotes:(NSString *)quotes
{
return [[words alloc] initWithQuotes:quotes];
}
Unless you can explain what [word Quotes:] method does, it should be much simpler than what you are trying to do.
_levelOne = #[#"First Quote.", "Second Quote.", #"And so on."];
-(void) randomQuotes{
NSInteger randInt = arc4random_uniform( [_levelOne count] );
self.quoteLabel.text = [_levelOne objectAtIndex:randInt];
}

Issue with setting picker view content

In my app a user can change the number of picker view elements by clicking some buttons. Everything is ok with that. But after that I want to show a word in it but the picker view is probably not ready and the app just crashes. Here is the cod for that
- (IBAction)changeNumberOfLettersToShow:(id)sender {
int numberOfLetters = (int)((UIBarButtonItem *)sender).tag;
switch (numberOfLetters) {
case 0:
[self setNumberOfLetters:[NSNumber numberWithInt: 10] andLanguage:nil];
break;
default:
[self setNumberOfLetters:[NSNumber numberWithInt: numberOfLetters] andLanguage:nil];
break;
}
[self showTheFirstWordForNumberOfLetters:numberOfLetters];
}
- (void) showTheFirstWordForNumberOfLetters: (int) numberOfLetters{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:numberOfLetters], [NSNumber numberWithInt:0], nil] forKey:#"currentPositon"];
NSArray *words = [allWordsForCurrentLanguage objectForKey:[NSString stringWithFormat:#"%#%d",[[NSUserDefaults standardUserDefaults] objectForKey:#"language"], numberOfLetters]];
[self displayWord:[[words objectAtIndex:0] uppercaseString] animated:YES];
}
- (void) setNumberOfLetters:(NSNumber *)numberOfLetters andLanguage: (NSString *) language {
[pickerHelper setNumberOfLettersToShow:numberOfLetters andLanguage:nil];
[self.picker reloadAllComponents];
}
- (void) displayWord:(NSString *)word animated:(BOOL)animated {
pickerHelper.pickerWorkingAutomatically = YES;
for (NSUInteger i = 0; i < [word length]; i++) {
NSString *letter = [NSString stringWithFormat:#"%c", [word characterAtIndex:i]];
[self displayLetter:letter
atComponent:i
animated:animated];
}
pickerHelper.pickerWorkingAutomatically = NO;
}
The picker looks similar to the one on the picture:
http://sourcefreeze.com/wp-content/uploads/2015/03/uipickerview-multi-selected-row.png
So I actually want to make a word from the fragments of a picker containing letters (which also user to work fine before I started doing it) and it actually works on a simulator.
UPDATED:
- (void) displayLetter:(NSString *)letter atComponent:(NSUInteger)component animated:(BOOL)animated {
NSUInteger row;
if (animated){
NSUInteger selectedRow = [self.picker selectedRowInComponent:component];
row = selectedRow + ([self.alphabet count] - selectedRow % [self.alphabet count]) + [self.alphabet indexOfObject:letter];
}
else
row = ((UINT16_MAX) / (2 * [self.alphabet count]) * [self.alphabet count]) + [self.alphabet indexOfObject:letter];
[self.picker selectRow:row
inComponent:component
animated:animated];
}

Call out of an Array, objectAtIndex

I have an Array with 10 Objects. I take the first Object and put it into my Label with a String.
Then I want to have a Method that increases the objectAtIndex by 1.
This is my Code :
//.m
#interface GameViewController () {
NSInteger _labelIndex;
}
#property (nonatomic) NSArray *playerArray;
#property (nonatomic) NSString *playerLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.playerArray = [NSArray arrayWithObjects:#"FIRST", #"SECOND", #"THIRD", #"FOURTH", #"FIFTH", #"SIXT", #"SEVENTH", #"EIGTH", #"NINTH", #"TENTH", nil];
_labelIndex = 0;
[self updateTurnLabel];
self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel];
}
Here I call the Method i another Method:
-(void) flipDraws {
self.boardView.userInteractionEnabled = NO;
[self updateTurnLabel];
CardView *cv1 = (CardView *) self.turnedDrawViews[0];
CardView *cv2 = (CardView *) self.turnedDrawViews[1];
}
This is my Method:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:_labelIndex % self.playerArray.count]; _labelIndex++;
}
I tried it with a for Loop but nothing happened. I tried it with just set the objectAtIndex:1 but my Method was not called.
What I am doing wrong?
{
int a;
}
- (void)viewDidLoad {
[super viewDidLoad];
a = 0;
self.playerArray = [NSArray arrayWithObjects:#"FIRST", #"SECOND", #"THIRD", #"FOURTH", #"FIFTH", #"SIXT", #"SEVENTH", #"EIGTH", #"NINTH", #"TENTH", nil];
self.playerLabel = [self.playerArray objectAtIndex:a];
self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel];
}
-(void) updateTurnLabel {
a +=1;
if (!a<[playerArray count])
{
a = 0;
}
self.playerLabel = [self.playerArray objectAtIndex:a];
}
call self.turnLabel.text = [NSString stringWithFormat:#"YOUR %# DRAW?", self.playerLabel]; after [self updateTurnLabel];
What are you adding +1 to in the method objectAtIndex:.
You should be maintaining a variable which tracks the current index being used, then in your method use this :
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:currentIndex+1];
}

Setting Timeout as Activity indicator keeps spinning when there is no response from webservice

I am using REST API to fetch data. I have two methods, one for ResponceSucces and other for responceFailure. In responce failure I simply dismiss the activity indicator. Sometimes it happens that data is not retrieved but still activity indicator keeps spinning infinitely. And ResponceFailure method is not Called.
My question is can I set timeout interval to my web service, to check if response did not come, then activity indicator must be dismissed after some time for example 20/30 seconds.
Below is my code:
-(void)messagesGetSuccess:(FBListingsWebHandler*)handler response:(NSDictionary*)response
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[FBModelManager sharedModelManager].messages removeAllObjects];
NSArray *messagesArray = [response objectForKey:#"message"];
if([[FBUserManager sharedUserManager] userType] == kUserShipper){
//extract usernames of transporters who placed bid on selected listing
NSArray *userNames;
NSMutableArray *transporterNames;
NSDictionary *biddingsDic = [[FBModelManager sharedModelManager] getModelDictionary:kModelBiddings];
if ([biddingsDic count] > 0){
userNames = [[biddingsDic allValues] valueForKey:#"userName"];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:userNames];
NSSet *uniqueNames = [orderedSet set];
transporterNames = [[NSMutableArray alloc] initWithArray:[uniqueNames allObjects]];
}
//add transporter records in messages dictionary who have messages for selected listing
for (int i = 1; i < [messagesArray count]; i++) {
NSMutableArray *modelArray = [[NSMutableArray alloc] init];
NSDictionary *groupedMsgDic = [messagesArray objectAtIndex:i];
int count = (int)[[[groupedMsgDic allValues] objectAtIndex:0] count];
for (int j = 0; j < count; j++) {
FBMessagesModel *model = [[FBMessagesModel alloc] initWithMessageItems:[[[groupedMsgDic allValues] objectAtIndex:0] objectAtIndex:j]];
[modelArray addObject:model];
model = nil;
}
[[FBModelManager sharedModelManager].messages setObject:modelArray forKey:[[groupedMsgDic allKeys] objectAtIndex:0]];
//remove transporter usernames from array who have messages besides bid for selected listing
if([transporterNames containsObject:[[groupedMsgDic allKeys] objectAtIndex:0]]){
[transporterNames removeObject:[[groupedMsgDic allKeys] objectAtIndex:0]];
}
modelArray = nil;
}
//add transporter records in messages dictionary who placed bid but have no message for selected listing
for (int i = 0; i < [transporterNames count]; i++) {
NSString *path = [[[biddingsDic allValues] objectAtIndex:[userNames indexOfObject:[transporterNames objectAtIndex:i]]] photoPath];
int transporterId = [[[biddingsDic allValues] objectAtIndex:[userNames indexOfObject:[transporterNames objectAtIndex:i]]] userId];
NSString *objectForZeroMsgs = [NSString stringWithFormat:#"%#-%d",path,transporterId];
[[FBModelManager sharedModelManager].messages setObject:objectForZeroMsgs forKey:[transporterNames objectAtIndex:i]];
}
transporterNames = nil;
// [self tableView:self.shipperGroupedMessagesTableView_ numberOfRowsInSection:0];
[self.shipperGroupedMessagesTableView_ reloadData];
}else if([[FBUserManager sharedUserManager] userType] == kUserTransporter){
for (int i = 1; i < [messagesArray count]; i++) {
FBMessagesModel *model = [[FBMessagesModel alloc] initWithMessageItems:[messagesArray objectAtIndex:i]];
[[FBModelManager sharedModelManager].messages setObject:model forKey:[NSString stringWithFormat:#"%d",model.mailId]];
if(model.mailMessage && ![model.mailMessage isEqualToString:#""]){
[self createMessageBubble:model];
}
model = nil;
}
[self.bubbleTableView_ reloadData];
}
}
-(void)messagesGetFailure:(FBListingsWebHandler*)handler
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[FBModelManager sharedModelManager].messages removeAllObjects];
if([[FBUserManager sharedUserManager] userType] == kUserTransporter){
[self.bubbleTableView_ reloadData];
}
else if([[FBUserManager sharedUserManager] userType] == kUserShipper){
if([[FBModelManager sharedModelManager].messages count] == 0){
NSDictionary *biddingsDic = [[FBModelManager sharedModelManager] getModelDictionary:kModelBiddings];
if ([biddingsDic count] > 0){
NSArray *userNames = [[biddingsDic allValues] valueForKey:#"userName"];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:userNames];
NSSet *uniqueNames = [orderedSet set];
NSArray *transporterNames = [uniqueNames allObjects];
[[FBModelManager sharedModelManager].messages removeAllObjects];
for (int i = 0; i < [transporterNames count]; i++) {
NSString *path = [[[biddingsDic allValues] objectAtIndex:[userNames indexOfObject:[transporterNames objectAtIndex:i]]] photoPath];
int transporterId = [[[biddingsDic allValues] objectAtIndex:[userNames indexOfObject:[transporterNames objectAtIndex:i]]] userId];
NSString *objectForZeroMsgs = [NSString stringWithFormat:#"%#-%d",path,transporterId];
[[FBModelManager sharedModelManager].messages setObject:objectForZeroMsgs forKey:[transporterNames objectAtIndex:i]];
}
[self.shipperGroupedMessagesTableView_ reloadData];
}else{
[self addNoMessagesLabel];
}
}
[self.shipperGroupedMessagesTableView_ reloadData];
}
}
I am adding a screenshot.

Sort UITableVIew after new string added

I need sort my UITableView when newly generated random strings are added.
I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addRandomString)];
self.navigationItem.rightBarButtonItem = addButton;
UIBarButtonItem *sortButton = [[UIBarButtonItem alloc] initWithTitle:#"Sort" style:UIBarButtonItemStyleBordered target:self action:#selector(sortStrings)];
self.navigationItem.leftBarButtonItem = sortButton;
stringsArray = [[NSMutableArray alloc]init];
int string_lenght = 10;
NSString *symbols = #"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";
for (int y = 0; y<8; y++)
{
NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];
for (int i = 0; i<string_lenght; i++)
{
[randomString appendFormat:#"%C", [symbols characterAtIndex:random()%[symbols length]]];
}
[stringsArray addObject:randomString];
}
stringsForTableView = [[NSMutableArray alloc]initWithArray:stringsArray];
}
- (void) addRandomString {
int string_lenght = 10;
NSString *symbols = #"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";
NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];
for (int i = 0; i<string_lenght; i++)
{
[randomString appendFormat:#"%C", [symbols characterAtIndex:random()%[symbols length]]];
}
[stringsForTableView addObject:randomString];
[self.tableView reloadData];
}
- (void) sortStrings{
??????????????????????
}
Thanks for your help!
Use the standard sort functions for mutable arrays:
[stringsForTableView sortUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES]]];
[self.tableView reloadData];

Resources