gpt4 book ai didi

ios - 在不同的 View Controller 中显示来自 TableView 单元格的标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:53 24 4
gpt4 key购买 nike

我似乎无法弄清楚这个非常简单的事情。我有一个带有 2 个按钮的 UIViewController,每个按钮都链接到不同的 UITableViewController,当我单击 UITableViewController 中的一个单元格时,我希望单元格的输入显示在按下的按钮中。输入来自数组。

我的一些代码:

主视图.m:

- (void)tableViewController:(TableViewController1 *)tableViewController didSelectRow (NSInteger)rowIndex 
{
NSLog(@"Selected row number: %d",rowIndex);
}

TableView1.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[self.delegate tableViewController:self didSelectRow:indexPath.row];
[self.navigationController popViewControllerAnimated:YES];
}

我得到了用这样的方法定义的按钮的标题:

MainView.m:

- (void)viewDidLoad
{

[self.industry setTitle:self.industryText forState:UIControlStateNormal];
[self.education setTitle:self.educationText forState:UIControlStateNormal];
[super viewDidLoad];
}

工业和教育本身就是按钮。 IndustryText 和 EducationText 是名称的占位符。

最佳答案

在有两个按钮的 MainView 中添加以下代码:

MainViewController.h

- (void)selectedFirstButtonText:(NSString *)strText;

MainViewController.m

在第一个按钮触摸事件上添加以下代码:

- (IBAction)btnFirstTouch:(id)sender {
FirstTableViewController *firstVC = [[FirstTableViewController alloc] init];
firstVC.delegate = self;
[self presentViewController:firstVC animated:YES completion:nil];
}

现在实现委托(delegate)方法:

- (void)selectedFirstButtonText:(NSString *)strText {
[self.btnFirst setTitle:strText forState:UIControlStateNormal];
}

FirstTableViewController.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@class MainViewController;

@interface FirstTableViewController : UITableViewController <UITableViewDataSource, UITableViewDataSource>
@property(nonatomic, assign) MainViewController *delegate;


@end

现在在您的 FirstTableViewController.m

@synthesize delegate = _delegate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

if([self.delegate respondsToSelector:@selector(selectedFirstButtonText:)]) {
[self.delegate selectedFirstButtonText:cell.textLabel.text];
NSLog(@"Selected Text");
}

[self dismissViewControllerAnimated:YES completion:nil];
}

Sample Project Dropbox Link

关于ios - 在不同的 View Controller 中显示来自 TableView 单元格的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16656665/

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