UIButton disappearing after button click - ios

When I click on a button in one row, the button in a different row disappears. Why might this be happening?
I looked at the following question and all the other questions within it, but nothing really answers my issue.
Custom, Imageless UIButton title disappears
I used the Debug Color Blended Layers to see if it's just a color thing, but my button just appears to disappear completely. I suspected this was a button.hidden property thing so I hardcoded button.hidden = NO; but nothing has changed.
What went wrong here?
Table Control Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([locationObjectsArray count] > 0)
{
return [locationObjectsArray count]+1;
}
else
return 1;
}
// Populate the Table View with member names
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the Cell...
UIButton *selectButton = (UIButton *)[cell viewWithTag:1];
UILabel *cityNamesText = (UILabel *)[cell viewWithTag:2];
UIButton *editButton = (UIButton *)[cell viewWithTag:3];
//NSLog(#"[locationObjectsArray count]: %lu", (unsigned long)[locationObjectsArray count]);
if (indexPath.row >= [locationObjectsArray count]) {
// locationObjectsArray count == 0; Empty Array
cityNamesText.text = #"Add New Location";
NSLog(#"%ld: %#", (long)indexPath.row, #"Add New Location");
editButton.hidden = NO;
[editButton setTitle:#"Add" forState:UIControlStateNormal];
//[editButton setTitle:#"Add" forState:UIControlStateApplication];
selectButton.hidden = YES;
}
else if ([locationObjectsArray count] > 0) {
LocationObject *locObject = [locationObjectsArray objectAtIndex:indexPath.row];
NSLog(#"%ld: %#", (long)indexPath.row, [locObject getLocationName]);
cityNamesText.text = [locObject getLocationName];
selectButton.hidden = NO;
editButton.hidden = NO;
}
// Assign button tags
selectButton.tag = indexPath.row;
editButton.tag = indexPath.row;
[selectButton addTarget:self action:#selector(selectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[editButton addTarget:self action:#selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
LocationObject *selectedLocationObject = [self loadLocationObjectWithKey:#"locObject"];
// Set Selected Cell to different Color
if ([cityNamesText.text isEqualToString:[selectedLocationObject getCityName]]) {
// Change to lightBlue color
UIColor * lightBlue = [UIColor colorWithRed:242/255.0f green:255/255.0f blue:254/255.0f alpha:1.0f];
[cell setBackgroundColor:lightBlue];
}
else
{
// All non-selected cells are white
//[cell setBackgroundColor:[UIColor whiteColor]];
//editButton.hidden = NO;
}
return cell;
}
// Select Button Clicked method
-(void)selectButtonClicked:(UIButton*)sender
{
if ([locationObjectsArray count] == 0)
{
NSLog(#"locObject count == 0");
// locationObjectsArray count == 0; Empty Array
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"No Locations Set"
message:#"Please add a new location."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
else
{
NSLog(#"locObject count > 0");
if (sender.tag >= locationObjectsArray.count) {
// Create local isntance of the selected locationObject
LocationObject *locObject = [locationObjectsArray objectAtIndex:sender.tag];
// Set locObject as current default locObject
[self saveLocationObject:locObject key:#"locObject"];
}
[mainTableView reloadData];
}
}
// Edit Button Clicked method
-(void)editButtonClicked:(UIButton*)sender
{
if ([locationObjectsArray count] == 0) {
// locationObjectsArray count == 0; Empty Array
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Add Location"
message:#"Input City Name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert addButtonWithTitle:#"Save"];
[alert show];
}
else
{
selectedObjectInArray = sender.tag;
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Edit Location"
message:#"Input City Name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert addButtonWithTitle:#"Save"];
[alert show];
}
}
// Handle alertView
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Add Location"]) {
// Add Location Alert View
if (buttonIndex == 0)
{
NSLog(#"You have clicked Cancel");
}
else if(buttonIndex == 1)
{
NSLog(#"You have clicked Save");
UITextField *cityNameTextField = [alertView textFieldAtIndex:0];
NSString *saveLocationName = cityNameTextField.text;
NSLog(#"saveLocationName: %#", saveLocationName);
if ([self isLocationValid:saveLocationName] == YES) {
NSLog(#"location is valid. locationObjectsArray.count = %lu", locationObjectsArray.count);
if (locationObjectsArray.count == 0) {
locationObjectsArray = [NSMutableArray array];
}
// City name input is valid
LocationObject *locObject = [[LocationObject alloc] init];
[locObject setCityName:saveLocationName];
locObject.byCityName = YES;
[locationObjectsArray addObject:locObject];
NSLog(#"After addObject: locationObjectsArray.count = %lu", locationObjectsArray.count);
[self saveLocationArrayObject:locationObjectsArray key:#"locationObjectsArray"];
[mainTableView reloadData];
}
else
{
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"City Name Invalid"
message:#"Unable to locate input city."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
}
else if ([alertView.title isEqualToString:#"Edit Location"])
{
// Edit Location Alert View
if (buttonIndex == 0)
{
NSLog(#"You have clicked Cancel");
}
else if(buttonIndex == 1)
{
NSLog(#"You have clicked Save");
UITextField *cityNameTextField = [alertView textFieldAtIndex:0];
NSString *saveLocationName = cityNameTextField.text;
if ([self isLocationValid:saveLocationName]) {
// City name input is valid
int selectedIndex = (int)selectedObjectInArray;
LocationObject *locObject = [locationObjectsArray objectAtIndex:selectedIndex];
[locObject setCityName:saveLocationName];
[locObject setByCityName:(Boolean *)TRUE];
[locationObjectsArray setObject:locObject atIndexedSubscript:selectedIndex];
[self saveLocationArrayObject:locationObjectsArray key:#"locationObjectsArray"];
[mainTableView reloadData];
}
else
{
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"City Name Invalid"
message:#"Unable to locate input city."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
}
}
Before:
After check button is selected:

Your issue is in these lines in cellForRowAtIndexPath:
// Assign button tags
selectButton.tag = indexPath.row;
editButton.tag = indexPath.row;
The tags will get mixed up as the cells are reused, I would recommend trying to omit using tags in this situation and use e.g. IBOutlets as #rdelmar pointed out.

Related

How to hide and unhide the UIAlertView.?

I am opening an alert view when the view will appear. My alertview style is UIAlertViewStylePlainTextInput.I am saving the textfield text in NSUserDefaults. I want when the textfield have text in their textfield alert is not open But if the textfield is empty only then the alert is pop up on the screen.I am using the below code.enter code here
- (void)viewDidLoad {
[super viewDidLoad];
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
proTextField.text = [[proAlert textFieldAtIndex:0]text];
}
}
proTextField = [[UITextField alloc]initWithFrame:CGRectMake(170, 35, 150, 40)];
proTextField.textColor=[UIColor blackColor];
//proTextField.placeholder = #"Pro/Tracking no";
NSUserDefaults *proNum = [NSUserDefaults standardUserDefaults];
proTextField.text = [proNum valueForKey:#"proTracking"];
[view2 addSubview:proTextField];
-(void)viewWillAppear:(BOOL)animated
{
[activity stopAnimating];
NSString *textString =[[proAlert textFieldAtIndex:0]text];
[proTextField.text length];
myText =textString;
NSLog(#"Textfield text - %#",myText);
NSUInteger length = [myText length];
NSLog(#"LENGTH of string %lu",(unsigned long)length);
if(myText<0)
{
proAlert.hidden = NO;
}
else
{
proAlert.hidden = YES;
}
}
-(void)viewWillAppear:(BOOL)animated {
if (txtField.text.length<=0) {
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
}

Move to another View on PickerView Selecting Row

I have a picker view that has values already inside, for example one of the things inside is weather, I have a button that works with the picker view so when u pick weather and push the button an alert pops up and says Welcome to X. You selected Weather than u push OK to remove the alert, I was wondering if it was possible to after you push OK it takes you to the view controller that has the weather. I have never seen this done and this is an all in one app, also there are about 6 or 7 other items on the picker so if someone could help that would be great: here is my code. By the way I am new to the site sorry if I didn't do Code Blocks.
- (void)viewDidLoad
{
NSArray *data = [[NSArray alloc] initWithObjects:#"Weather", #"Calendar", #"Facebook", #"Twitter", #"Instagram",#"Tasks" ,nil];
self.array = data;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#pragma marks Picker Data Source Methods
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [_array count];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [_array objectAtIndex:row];
}
- (IBAction)ButtonPressed:(id)sender {
NSString *select = [_array objectAtIndex:[_picker selectedRowInComponent:0]];
NSString *title = [ [NSString alloc] initWithFormat:#"You selected %#!" , select];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:#"Welcome To One" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
You have to do like bellow need to give tag to UIAlertView otherwise it will push view on each UIAlertView click
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSString *select = [_array objectAtIndex:component];
NSString *title = [ [NSString alloc] initWithFormat:#"You selected %#!" , select];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:#"Welcome To One" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
alert.tag = 1001;
alert.delegate = self;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1001)
{
[self.navigationController performSegueWithIdentifier:#"Identifier" sender:nil];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// Show alert here
[[[UIAlertView alloc]
initWithTitle:#"" message:#"Alert Message"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil]
show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
UIStoryboard * storyboard = self.storyboard;
ViewController * detail = [storyboard instantiateViewControllerWithIdentifier: #"YourViewControlller"];
[self.navigationController pushViewController: detail animated: YES];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSString *select = [_array objectAtIndex:component];
NSString *title = [ [NSString alloc] initWithFormat:#"You selected %#!" , select];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:#"Welcome To One" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
alert.delegate = self;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self.navigationController performSegueWithIdentifier:#"Identifier" sender:nil];
}
}

Giving function for more uiSwitch tags

I would like to set that if all of my switches for sender tag 0-6 will be off, app shows alert with warning message to put at least one of the switches on. I am now little bit lost with logic how to.
if (switi.tag ==0 && switi.isOn == YES )
{ [self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
}else if ((switi.tag ==1 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==2 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==3 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==4 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==5 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else if ((switi.tag ==6 && switi.isOn == YES)) {
[self.navigationController popViewControllerAnimated:YES];
[self willMoveToParentViewController:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please put at least one switch on." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
EDIT
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat y = 110;
int counter = 0;
for (NSString *key in self.kejsss) {
UILabel *labeli = [[UILabel alloc] initWithFrame:CGRectMake(50, y, 150, 30)];
UISwitch *switi = [[UISwitch alloc] initWithFrame:CGRectMake(210, y, 70, 30)];
labeli.tag = counter;
switi.tag = counter;
labeli.text = key;
labeli.textColor = [UIColor whiteColor];
switi.on = [[self.konfiggg objectForKey:key] boolValue];
[switi addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:labeli];
[self.view addSubview:switi];
self.viewtabulka.layer.borderColor = [UIColor whiteColor].CGColor;
self.viewtabulka.layer.borderWidth = 0.5;
CALayer *imageLayerRR3 = self.viewtabulka.layer;
[imageLayerRR3 setCornerRadius:5];
[imageLayerRR3 setBorderWidth:1];
[imageLayerRR3 setMasksToBounds:YES];
y += 40;
counter++;
}
}
}
}
- (IBAction)switchChanged:(UISwitch *)sender{
kokos= YES;
[self.konfiggg setObject:#([sender isOn]) forKey:[self.kejsss objectAtIndex:sender.tag]];
NSLog(#"Selected Switch - %ld (%#)", (long)sender.tag, self.kejsss[sender.tag]);
NSLog(#"Selected object - %d", [sender isOn]);
}
Here is one way. Group all of your switches together in an array for convenience. Then whenever one of the switches changes value, call a method that loops through all of the switches in the array and checks to make sure at least one is still on. You could do it like this:
-(void)checkSwitches {
// self.switches is an NSArray that holds pointers to all of your UISwitches
for (UISwitch *s in self.switches) {
if (s.on == YES) {
return;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please put at least one switch on." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

Do not pass cursor to next textfield without entering value in first textfield

I have created textfields dynamically in UITableView I don't want to pass cursor without entering value in first textfield
[self.playerTable.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL*stop)
{
UITableViewCell *cell = obj;
if([cell isKindOfClass:[UITableViewCell class]])
{
for(UITextField *textField in cell.contentView.subviews)
{
if([textField isKindOfClass:[UITextField class]])
{
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
isEditMode = NO;
if(!isEditMode && [playerstr length] > 0)
{
NSMutableArray *playerinfoArry = [dbWrapper getPlayerInfo];
for (Playerinfo *player in playerinfoArry)
{
if ([player.playername isEqualToString:playerstr])
{
isPlayerExist = YES;
isEditMode = !isEditMode;
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[_nameField resignFirstResponder];
[alert showInView:self.view];
NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:selectedRow inSection:0];
[_playerTable selectRowAtIndexPath:indexPath1 animated:YES scrollPosition:UITableViewScrollPositionTop];
return;
}
}
}
}
}
}
}
}];
instead of looking for the first responder while trying to edit another UITextField, have you tried the other approach: not permitting the UITextField to resign as first responder. This could be something like:
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField{
for(UITextField *otherTextField in self.view)
{
if ([otherTextField isKindOfClass:[UITextField class]] && [textField.text isEqualToString:otherTextField.text]){
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[alert showInView:self.view];
return NO;
}
}
return YES;
}
I hope this helps.

Uialertview text to NSMutableArray, ios 4.3

i have been trying to copy text input from alertview textfield to a NSMutableArray that i will use later, alertview pops up and i enter the input to text field but when i press OK alert view disappears but doesnt copy text to my mutable array
here is my code
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
[dialog addSubview:nameField];
if ([nameField text]){
NSLog(#"Name Field %# ",nameField.text);
[addCustomStand addObject:nameField.text];
}
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
here is my output on log screen
2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected.
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
""
)
anyone can help whats wrong with that code?
That's because [nameField text] doesn't have user entered value yet when you added it in your [addCustomStand addObject:nameField.text];
so change your adding into array in UIAlertView delegate method.
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
// Note at this line
nameField.tag = 100;
//
[dialog addSubview:nameField];
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
// Note at this line
UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
[addCustomStand addObject:nameField.text];
//
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
You are adding nameField.text to your addCustomStand array before you even show the alert dialog. At the time you add it to the array the value is an empty string.
Instead you need to copy the value into your array during your clickedButtonAtIndex: method, by doing something like this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSString *location;
UIView *view;
for (view in [alertView subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
location = [(UITextField*)view text];
}
}
if (location) {
[addCustomStand addObject:location];
}
}
}

Resources