I have a view controller with 6 buttons on it. Each of these buttons push a single table view controller which will be propagated with items depending on what value the button had. Lets say the buttons were ‘car’, ‘van’ etc. is it possible to remember the value of the button when the table view is pushed so that the twitter search can be based on the value handed over by the button i.e #car? I can do this with 6 different table views as I can just assign a viewDidLoad method to each based on the search but I would rather only do it once and allow the table view to ‘fill in’ the value on the button automatically. Here is my code:
- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"https://api.twitter.com/1/statuses/public_timeline.json"]];
NSError* error;
tweets = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:@"text"];
NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", name];
return cell;
}
Easy man. Set a public property on that TableViewController to hold that value:
In the TVC .h file:
And synthesize it in the TVC .m file
If you are using Storyboard, just make sure you have the segue wired up to the ViewController itself and NOT to the buttons and then in each of the buttons IBActions do something like:
In the
prepareForSegueMethod(implement if you haven’t already:Then do whatever you want to with that value in your TableViewController
* EDIT *
For a custom attribute on an object (like a UIButton). Add a new file to your project (I put them in a group called Custom Subclasses). This file should be of UIButton class. Name it TweetButton.
Then replace what you have in TweetButton.h with this:
import
@interface TweetButton : UIButton
@property(nonatomic, strong) NSString *buttonName;
@end
TweetButton.m should look like:
import “TweetButton.h”
@implementation TweetButton
@synthesize buttonName;
@end
Then just change the parent class of each of those buttons to TweetButton instead of UIButton (this will be done in Interface Builder).
Then in each of the IBActions, cast that button to type of TweetButton and access/set the name property.
After going through all this, another idea would be to just add in a property (NSString) in the ViewController that is calling the segue (the one with the buttons) and set that to whatever you want and then use that to send to the destination VC.