gpt4 book ai didi

iphone - 按下 TableView 时单击按钮以记住值

转载 作者:行者123 更新时间:2023-11-29 04:40:24 25 4
gpt4 key购买 nike

我有一个 View Controller ,上面有 6 个按钮。这些按钮中的每一个都会推送一个 TableView Controller ,该 Controller 将根据按钮具有的值来传播项目。假设按钮是“汽车”、“货车”等。当按下 TableView 时是否可以记住按钮的值,以便 Twitter 搜索可以基于按钮传递的值(即 #car)?我可以使用 6 个不同的 TableView 来执行此操作,因为我可以根据搜索为每个 TableView 分配一个 viewDidLoad 方法,但我宁愿只执行一次并允许 TableView “填充”值自动在按钮上。这是我的代码:

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

最佳答案

轻松的人。在该 TableViewController 上设置一个公共(public)属性来保存该值:

在 TVC .h 文件中:

@property(nonatomic,strong) NSString *selectedButtonText;

并将其合成到TVC .m文件中

@synthesize selectedButtonText;

如果您使用 Storyboard,只需确保将 Segue 连接到 ViewController 本身而不是按钮,然后在每个按钮中 IBAction 执行如下操作:

[self performSegueWithIdentifier@"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)的自定义属性。将新文件添加到您的项目中(我将它们放在名为“自定义子类”的组中)。该文件应该是 UIButton 类。将其命名为 TweetButton。

然后将 TweetButton.h 中的内容替换为:

导入

@interface TweetButton:UIButton@property(非原子,强) NSString *buttonName;@结束

TweetButton.m 应如下所示:

导入“TweetButton.h”

@implementation TweetButton@synthesize按钮名称;@结束

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

然后在每个 IBAction 中,将该按钮转换为 TweetButton 类型并访问/设置 name 属性。

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

关于iphone - 按下 TableView 时单击按钮以记住值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com