Reputation: 631
- (void)viewDidLoad {
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton { // Method for creating button, with background image and other properties
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show More" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 500.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)aMethod {
NSLog(@"in load more n===>>>%d",n);
[tbl reloadData];
}
There is an NSMutableArray
containing thirty objects. First only ten objects should be shown, and after clicking on the 'show more' button twenty objects will show, and after clicking a second time on the button then thirty objects should be shown and the button title will become 'Return'. When I click on 'Return' then the table view should only show twenty objects again. After the next click ten elements should be shown.
Upvotes: 0
Views: 796
Reputation: 631
-(void) aMethod{
if(n==2)
{
[button setTitle:@"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
// n--;
}
else
{ [button setTitle:@"Show More" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
}
NSLog(@"n==>> %d",n);
[tbl reloadData];}
Here is my code.
Upvotes: 1
Reputation: 1489
You can do this work by three ways
1: Declare your button in private interface in implementation file, so that you can access the button anywhere in the class.
2: Declare aMethod() with the (id)sender parameter and access type the sender to UIButton.
3: Add the tag property of UIButton in AddMyButton button.tag = 1;
and reference the button in aMethod
-(void)aMethod
{
UIButton *button2 = (UIButton *)[self.view viewWithTag:1];
[button2 setTitle:@"return" forState:UIControlStateNormal];
NSLog(@"in load more n===>>>");
[tbl reloadData];
}
Upvotes: 0
Reputation: 2221
Your button is dynamically attached, you must create a reference to it.
You can use a counter, like this:
-(void) aMethod{
if(ctr < arrayLength){
ctr++;
[buttonReference setText:@"Show More"];
} else {
ctr--;
[buttonReference setText:@"Return"];
}
.....
}
Upvotes: 0