2012-05-07 40 views
0

我有在其上6个按钮的视图控制器。这些按钮中的每一个都会推送一个表格视图控制器,该控制器将根据按钮具有的值进行传播。比方说,按钮是'汽车','面包车'等等,是否有可能在推送表格视图时记住按钮的值,以便可以基于由按钮交付的值,即#car,来搜索推文。我可以用6种不同的表格视图来完成这项工作,因为我可以根据搜索为每个方法分配viewDidLoad方法,但我宁愿只做一次,并允许表格视图自动“填充”按钮上的值。这里是我的代码:按钮点击记住值时表视图被推

- (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; 
} 
+1

您可以在主控制器上创建6个用于推文的数组,您可以通过单击按钮来推送下一个视图,现在可以搜索该数组。 – rishi

+0

感谢Rishi - 当表格视图被按下时,即当它从视图控制器移动到表格视图时,阵列是否会自动清空? – Alan

+1

阵列将在您的主视图控制器弹出时清空,并且根据您的问题,主视图似乎不会在所需的场景中弹出。 – rishi

回答

1

容易的人。对TableViewController设置一个公共属性来保存值:

在TVC .h文件中:

@property(nonatomic,strong) NSString *selectedButtonText; 

而且合成它在TVC .m文件

@synthesize selectedButtonText; 

如果您正在使用故事板,只要确保你有SEGUE线连到ViewController本身,而不是到按钮,然后在每个按钮IBActions的做这样的事情:

[self [email protected]"mySegueID" sender:sender]; 

prepareForSegueMethod(实现,如果你还没有准备好:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([segue.identifier isEqualToString:@"mySegueID"]) { 
     // Cast the sender as a UIButton to get the text 
     UIButton *tappedButton = (UIButton *)sender 

     MyTableViewController *mtvc = segue.destinationViewController; 
     mtvc.selectedButtonText = tappedButton.titleLabel.text; 
    } 
} 

然后做任何你想做的与值在TableViewController

*编辑*

对于定制属性(如UIButton)。添加一个新文件到你的项目中(我把它们放在一个名为Custom Subclasses的组中)。这个文件应该是UIButton类的。将其命名为TweetButton。

然后用这个代替你有什么TweetButton.h:

进口

@interface TweetButton:UIButton的 @property(非原子,强)的NSString * BUTTONNAME; @end

TweetButton.m应该是这样的:

进口 “TweetButton.h”

@implementation TweetButton @synthesize BUTTONNAME; @end

然后,只需将每个按钮的父类更改为TweetButton而不是UIButton(这将在Interface Builder中完成)。

然后在每个IBActions的,流延该按钮键入TweetButton和访问/设置的名称属性。

经过所有这些之后,另一个想法是在ViewController中添加一个属性(NSString),该属性调用segue(带按钮的那个)并将其设置为任何你想要的,然后用它来发送到目的地VC。

相关问题