how to load same tableview with different NSArrays depending on uibuttons tapped? - uitableview

In my app i have a scroll-view and a table-View added as a sub-view and on this scroll view i have multiple menu buttons (UI Button) added as sub-view to my scroll-view.i want to load the table-View with different arrays depending on respective menu button is tapped.Any Ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
[self PutButtoninScrollView:7];
}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
myButton1=[[UIButton alloc]init];
myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
myButton1.frame=CGRectMake(5, 5, 70, 30);
[myButton1 setTitle:#"दुनिया" forState:UIControlStateNormal];
[myButton1 setTag:1];
myButton1.backgroundColor=[UIColor blackColor];
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton1];
myButton2=[[UIButton alloc]init];
myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2.frame=CGRectMake(80, 5, 70, 30);
[myButton2 setTitle:#"India" forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton2];
[self.myScrollView setContentSize:CGSizeMake(160, 35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([myButton.currentTitle isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([myButton.currentTitle isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
problem is when i execute the code
"successful" is displayed. that means ListViewAction method is getting called but my if statement is never executed.

try this
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{
myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(xPosition, 5, 70, 30);
myButton.backgroundColor=[UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[myButton setTag:i];
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton];
xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
- (void) buttonClicked:(id) sender
{
NSString *strTitle = [sender currentTitle];
// arrItem have list of array items
NextView *detailViewController = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
}

-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([[sender currentTitle] isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}

try this
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];

Related

Issue with UIButton Title

I am fetching the data from UITableViewCell and am storing into string is countryString. I want to change the button title with country string but its not working, i thought it is a simple issue, but am struggling from last 2 hours. still I cant find the solution why is not working
countryButton=[[UIButton alloc]initWithFrame:CGRectMake(145, 145, 100, 30)];
countryButton.backgroundColor=[UIColor whiteColor];
[countryButton setTitle:CountryString forState:UIControlStateNormal];
countryButton.titleLabel.textColor=[UIColor blackColor];
[countryButton addTarget:self action:#selector(countryTable) forControlEvents:UIControlEventTouchUpInside];
[notesView addSubview:countryButton];
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
jsonDict = [newarray objectAtIndex:indexPath.row];
CountryString=[jsonDict objectForKey:#"countryName"];
NSLog(#"country is %#", CountryString);
CountryTableview.hidden=YES;
}
try this
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
jsonDict = [newarray objectAtIndex:indexPath.row];
CountryString=[jsonDict objectForKey:#"countryName"];
NSLog(#"country is %#", CountryString);
[countryButton setTitle:CountryString forState:UIControlStateNormal];
countryButton.titleLabel.textColor=[UIColor blackColor];
CountryTableview.hidden=YES;
}
assume that its your str
CountryString =#"XXXX";
UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //or button type custom
[countryButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[countryButton setFrame:CGRectMake(145, 145, 100, 30)];
[countryButton setTitle: CountryString forState:UIControlStateNormal];
[countryButton setExclusiveTouch:YES];
countryButton.backgroundColor=[UIColor whiteColor];
countryButton.titleLabel.textColor=[UIColor blackColor];
[self.view addSubview: countryButton]; //whatever u need self.view or notesView
-(void) buttonClicked:(UIButton*)sender
{
NSLog(#"you clicked on button %#", sender.tag);
}

Undeclared identifier "viewDidLoad"

//Objects list
//objects scroll
UIScrollView *objects;
objects=[[UIScrollView alloc]initWithFrame:CGRectMake(0,80,512,688)];
objects.showsVerticalScrollIndicator=YES;
objects.scrollEnabled=YES;
objects.userInteractionEnabled=YES;
objects.backgroundColor = [UIColor clearColor];
[self.view addSubview:objects];
[objects setUserInteractionEnabled:YES];
[objects setCanCancelContentTouches:YES];
objects.contentSize = CGSizeMake(512,689);
//divider
UIButton *divider = [UIButton buttonWithType:UIButtonTypeCustom];
[divider setBackgroundImage:[UIImage imageNamed:#"white.png"]forState:UIControlStateNormal];
divider.frame = CGRectMake(300, 0, 1, 768);
[divider setTitle:#"" forState:UIControlStateNormal];
[self.view addSubview:divider];
divider.adjustsImageWhenHighlighted = NO;
//array colors
UIImage *color[3000];
//color setup
color[0] = [UIImage imageNamed:#"item.png"];
UIImageView *originalImageView = [[UIImageView alloc] initWithImage:color[0]];
[originalImageView setFrame:CGRectMake(300, 0, 212, 62)];
[objects addSubview:originalImageView];
//array buttons
UIImageView *button[3000];
//button setup
button[0] = [[UIView alloc] initWithFrame:[originalImageView frame]];
UIImageView *maskImageView = [[UIImageView alloc] initWithImage:color[0]];
[maskImageView setFrame:[button[0] bounds]];
[[button[0] layer] setMask:[maskImageView layer]];
button[0].backgroundColor = [UIColor blueColor];
[objects addSubview:button[0]];
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the updated code, some before, and everything after. There's a lot more to the program but it is all just setting up different visual elements for a GUI. The error is tagged on the line that sais..
-(void)viewDidLoad {
And the error sais this..
Use of undeclared identifier 'viewDidLoad'
Would you like more info??
you cannot have method inside another method.
this is part of your code
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}
} // <----this should not be here
change it to
//add object button
UIButton *plus = [UIButton buttonWithType:UIButtonTypeCustom];
[plus setBackgroundImage:[UIImage imageNamed:#"plus.png"]forState:UIControlStateNormal];
plus.frame = CGRectMake(394, 106, 25, 25);
[plus setTitle:#"" forState:UIControlStateNormal];
[objects addSubview:plus];
plus.adjustsImageWhenHighlighted = YES;
} // <--- should be here to end method
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:#"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}

executing an action in a different view controller

In my application, I am trying to create a button, with a button. My code right now is as follows:
- (IBAction)doneTextField:(id)sender {
YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
YSVC2.stringFromTextField = self.textField.text;
[self presentViewController:YSVC2 animated:YES completion:nil];
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn addTarget:self action:#selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"FaceTime" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
}
this works, but I need the button that is being added to add in a different view controller, not the view controller that the code is in.
How would I do this??
Thank you
You need to create a function to add the button in the new view controller:
In YourShortcutsViewController.h:
-(void)addFaceTimeButton:(UIButton*);
In YourShortcutsViewController.m:
-(void)addFaceTimeButton:(UIButton*){
[self.view addSubview:btn];
}
And then change your code to:
- (IBAction)doneTextField:(id)sender {
YourShortcutsViewController *YSVC2 = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
YSVC2.stringFromTextField = self.textField.text;
[self presentViewController:YSVC2 animated:YES completion:nil];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn addTarget:self action:#selector(newButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"FaceTime" forState:UIControlStateNormal];
[YSVC2 addFaceTimeButton:btn];
}

how to remove duplicate button

This is my button creation function.
-(void)buttonCreate {
//oneBtn5 Button
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}
I will show button using viewonload(),
- (void)viewDidLoad {
[super viewDidLoad];
[self buttonCreate];
}
This is my restart function, if i clicked the restart, new button will create.
- (void)RestartAction {
[self buttonCreate];
}
My problem is how to remove duplicate button when i click the restart. It's there any way to solve this problem.
-(void)buttonCreate {
//oneBtn5 Button
if (oneBtn5 != null)
[oneBtn5 removeFromSuperview];
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}
Just remove the button from it's superview:
-(void)buttonCreate {
if (oneBtn5.superView) {
[oneBtn5 removeFromSuperview];
}
....
}
you could do this in buttonCreate
if(oneBtn5==nil)
{
oneBtn5 = [UIButton buttonWith....];
[self.view addsubview: oneBtn5;
}
button only inits and adds once
Use this as :
-(void)buttonCreate {
if ([self.view.subviews containsObject:oneBtn5]) {
[oneBtn5 removeFromSuperview];
}
// YOUR CODE HERE
}
If it is a property, as people tell you, check if it exists previously.
if(!self.oneBtn5){
//instance of button
self.oneBtn5 = [UIButton buttonWith....];
[self.view addsubview: self.oneBtn5];
}else{
//change stuff you need like text or frame
}
The problem is that you are creating new instances of the button instead of reusing the previous one.
-(void)buttonCreate {
//oneBtn5 Button
if (oneBtn5 ){
[oneBtn5 removeFromSuperview];
oneBtn5=nil; // release the memory
}
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}
You must write oneBtn = nil after [oneBtn5 removeFromSuperview] to release the memory for the previously created button. As [oneBtn5 removeFromSuperview]; remove the button from its superview but does not release memory.You must release the memory by setting it to nil.

How to change button image added to TableView section header

I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}

Resources