Action for button in cell - Objective C - button

I'm a beginner in Objective-C. I have a problem.
I have a tableview with 20 cells, it has "name" and "button" for each cell. I want when I click the button in cell, that button will be changed background image. But I don't know why, when I click button in cell #1 it change background but button in cell #8 also auto change background. Anyone have any idea? Please help me.
-(void)clickLike:(UIButton*)sender
{
UIImage *curImg = sender.currentImage;
UIImage *like = [UIImage imageNamed:#"like.png"];
UIImage *dislike = [UIImage imageNamed:#"dislike.png"];
if(curImg==like){
[sender setImage:[UIImage imageNamed:#"dislike.png"] forState:UIControlStateNormal];
sender.selected = TRUE;
//Insert dislike song
SongObject *list = [self.arrSong objectAtIndex:sender.tag];
idUser=#"1";
idSong=list.idsong;
[self dislikeSong:idSong and:idUser];
}
else if(curImg==dislike){
[sender setImage:[UIImage imageNamed:#"like.png"] forState:UIControlStateNormal];
sender.selected = TRUE;
//Insert like song
SongObject *list = [self.arrSong objectAtIndex:sender.tag];
idUser=#"1";
idSong=list.idsong;
[self likeSong:idSong and:idUser];
}
}

button in cell #1 and button in cell #8 are same UIButton Object, the problem is not in this method. You must have two button pointed to one UIButton Object. Check the code where button #1 and button #8 are created.

Related

Popover button not showing in split view app

I have a split view app that should show a menu button when the device is in portrait orientation and the Master view is hidden.
When the app launches in portrait mode, this function is executed:
- (void)splitViewController:(UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController:(UIPopoverController*)pc {
barButtonItem.title = #"Menu";
self.popoverController = pc;
self.rootPopoverButtonItem = barButtonItem;
// Get current detail view (splitVC-->detailNavVC-->currentVC).
UINavigationController *detailNav = [self.splitController.viewControllers objectAtIndex: 1];
UIViewController <SubstitutableDetailViewController> *detailViewController = [detailNav.viewControllers objectAtIndex: 0];
[detailViewController showRootPopoverButtonItem: rootPopoverButtonItem];
}
This function in turn launches the function in TitleViewController.m:
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
// Add the popover button to the toolbar.
NSMutableArray *itemsArray = [toolbar.items mutableCopy];
[itemsArray insertObject: barButtonItem atIndex: 0];
[toolbar setItems:itemsArray animated:NO];
}
All of this code seems to run correctly, but no menu button is showing. Any ideas? Thanks.
I seemed to have solved the problem by adding the following line after setting the toolbar items:
[self.view reloadInputViews];

UIImagePickerController in UIPopoverController - location not working

In an iPad app I'm writing I have button in a static table cell that launches an image picker inside a popover.
I want the popover's arrow to point at the button that launched the popover. Right now, when I tap the button it doesn't. It throws the popover on screen (with the working image picker) but the arrow either points to the top of the screen or kind of anywhere else (can't figure out if it's random or not--see below).
The tableview that holds the static cell that contains the button has its scroll disabled but is located on a scroll view. Could that cause random arrow locations?
Here is my code:
// present image picker in a popover
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePicker.allowsEditing = NO;
_imagePicker.navigationBar.tintColor = [UIColor redColor];
_imagePicker.navigationBar.backgroundColor = [UIColor greenColor];
_popover = [[UIPopoverController alloc] initWithContentViewController:_imagePicker];
[_popover presentPopoverFromRect:self.theButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Can someone please explain why the arrow isn't pointing at the button's frame and/or how to fix it?
Thanks.
The button is likely not a direct subview of self.view.
Try:
[_popover presentPopoverFromRect:self.theButton.frame inView:self.theButton.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Change title of bar button in UIImagePickerController's navigation bar

my question is simple: how can i change change title of bar button in UIImagePickerController's navigation bar?
I created the controller for UIImagePickerController (triggered by the pressure onto a button) and then tried to modify its properties:
- (IBAction)chooseFromRoll:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.navigationBar.tintColor = [UIColor colorWithRed:0.42 green:0.18 blue:0.66 alpha:1.0];
//imagePicker.navigationItem.title = #"Scegli foto profilo";
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[imagePicker.navigationController.navigationItem.rightBarButtonItem setTitle:#"Annulla"];
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
but nothing changed, only navigation bar was correctly modified.
Here's the code for function willShowViewController:
- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
label.textAlignment = 2;
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width-200,label.frame.size.height);
label.adjustsFontSizeToFitWidth = YES;
label.font = [UIFont fontWithName:#"Futura" size:15.0];
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:#"Scegli foto profilo"];
[navigationController.navigationBar.topItem setTitleView:label.viewForBaselineLayout];
[navigationController.navigationBar.topItem.rightBarButtonItem setTitle:#"Annulla"];
}
can you tell me where's the mistake?
thanks
Here is a code snippet for twicking the title of the navigation bar in uiimage picker.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *ipcNavBarTopItem;
// add done button to right side of nav bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Photos"
style:UIBarButtonItemStylePlain
target:self
action:#selector(saveImages:)];
UINavigationBar *bar = navigationController.navigationBar;
[bar setHidden:NO];
ipcNavBarTopItem = bar.topItem;
ipcNavBarTopItem.title = #"Photos";
ipcNavBarTopItem.rightBarButtonItem = doneButton;
}
Hope this would help you out.
EDIT:
the answer to first question is yes you can use the ibaction or just use the method i provided above to create the button, both should work. if you are using inaction, make sure you declare you controller within it so you can make it appear and dismiss it from within the action method. the answer to the second question if you want to change the title of the view that is presented when image picker from the photo album is presented then you need to create a new subview and pop it up to allow the user to select the desired image then you can change the title the way i did it above. lengthy process, i would not recommend it. the answer to the third question, try using the following code snippet where you declare your button,
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
where the btn is the name of your button and you can modify the font name and the size. i hope my answers can help you. good luck and let me know if you need any more help.

xcode 4 dismissModalViewControllerAnimated

In my app,I switch value between views [dismissModalViewControllerAnimated].
I have one text and one button in the first viewcontroller.When i press button if text empty,gives on alert view.
If text full,to calculate the second view Label.it's works.
But when i mov from the first viewcontroller to the second viewcontroller,the value of the text is written in before on passing.
So, when i press back and press button second time,I see correct value in Label.
Can i help me please
FirstViewController.m
(IBAction)go:(id)sender {
if ([aText.text length]>0)
{
SecondViewController *lvc =[self.storyboard instantiateViewControllerWithIdentifier:#"second"];
[self presentModalViewController:lvc animated:YES];
}
else {
UIAlertView *message =[[UIAlertView alloc] initWithTitle:#"Warning" message:#"Please enter value" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[message show];
[self dismissModalViewControllerAnimated:YES];
}
SecondViewController.m
(IBAction)back:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
value of the variable my formul: [atext floatvalue] * 2 while the text empty,and i input text 10 and after press button Label is NAN press back button, goto first view controller again press GO button Label is 20
Sorry but I can't understand what you're trying to do (and I've tried!).
If you want to pass a value from a ViewController to another, you should do something like this:
SecondViewController *lvc =[self.storyboard instantiateViewControllerWithIdentifier:#"second"];
lvc.value = yourValue;
[self presentModalViewController:lvc animated:YES];

In RootViewController my UIBarButtonItem disappears when using UITableViewStyleGrouped

I'm using a split view for an iPad app.
In my RootViewController.m file I assign two UIBarButtonItems like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 300.0);
// add a save button
saveButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:#selector(saveAction:)] autorelease];
self.navigationItem.rightBarButtonItem = saveButton;
// add a cancel button
cancelButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(cancelAction:)] autorelease];
self.navigationItem.leftBarButtonItem = cancelButton;
}
These buttons show up nicely when I use UITableViewStylePlain styling for the table view. But if I use UITableViewStyleGrouped, then the buttons disappear. Here is how I set my grouped style
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithStyle:UITableViewStyleGrouped];
return self;
}
Regardless of the presence or absence of the buttons, the size of the header remains the same.
I'm clueless as to why this happening.
all help greatly appreciated
Dhoti

Resources