试图从一个 Viewcontroller 中的 UILabel 获取 NSString 以放置在另一个 Viewcontroller 的 UITextView 中

     2023-03-11     22

关键词:

【中文标题】试图从一个 Viewcontroller 中的 UILabel 获取 NSString 以放置在另一个 Viewcontroller 的 UITextView 中【英文标题】:Stuck trying to get the NSString from a UILabel in one Viewcontroller to place in a UITextView of another Viewcontroller 【发布时间】:2012-07-21 19:01:22 【问题描述】:

我有一个带有两个视图控制器的应用程序,我想在 VC2 内部的 UITextView 中显示来自 VC1 中的 UILabel 的文本。

我已经能够在 VC2 的 viewDidLoad 中从 VC1 访问 UILabel,但实际文本没有出现。

在 VC2 中我创建了一个 VC1 对象并且可以访问 VC1。这是 VC2 viewDidLoad:

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.textView.text = vc1Controller.label.text;

但是当我运行应用程序并出现 VC2 时,UITextView 中什么也没有出现。我确实已经连接了它,并且可以通过执行 NSLog(@"Yes"); 来显示文本;比如上面的方法。

当我做一个 NSLog(@”%@”, vc1Controller.label.text);我在输出中得到 NULL。

我需要创建自定义 getter 吗?任何建议表示赞赏。我对所有编程都比较陌生。

【问题讨论】:

“[vc1Controller self.label.text]”有效吗? 您是在哪里以及如何创建 VC1 的?您是如何创建标签的? 我更改了上述内容,我错误地发布了我当前的代码。在 VC2.m 内部,我创建了一个 VC1 的对象。标签是根据用户输入在 VC1 中创建的。 label.text = [label.text stringByAppendingString: 用户输入];但是一旦创建,我会尝试在新的 VC 中显示文本。 【参考方案1】:

这是从视图中传递 NSString 的方式:

第一个视图:

@interface ViewController : UIViewController
IBOutlet UILabel *firstLbl;
NSString *firstString;


-(IBAction)labelTouched:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

firstString = @"I'm the first labels text";
firstLbl.text = firstString;




-(IBAction)labelTouched:(id)sender

ViewController2 *view2 = [[ViewController2 alloc] init];
view2.selectedFirstLabelString = firstString;
[self presentModalViewController:view2 animated:YES];
[view2 release];

第二视图:

@interface ViewController2 : UIViewController
IBOutlet UILabel *secondLbl;

@property (nonatomic, retain)NSString *selectedFirstLabelString;

-(IBAction)done:(id)sender;
@end



@implementation ViewController2
@synthesize selectedFirstLabelString;


- (void)viewDidLoad

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

secondLbl.text = selectedFirstLabelString;

希望这会有所帮助。

【讨论】:

我确实尝试过这个并且 vc1Controller.textString 是(null)。在 VC1 中,我做了一个 NSLog 并且 textString 输出正确,但在 VC2 中却没有。 您是使用 TabBarController 来处理单独的视图,还是在导航控制器中传递视图? 两者都不是。我的项目最初是一个单视图应用程序,我添加了一个新的 UIViewController。当用户在 VC 1 中触摸标签时,它会触发一个操作表,清除标签作为破坏性选项或“查看”以查看标签的内容。如果他们选择“查看”,我想添加一个子视图(VC2)来显示标签的全部内容。我现在有了这个设置,“视图”选项只是改变了 VC1 中 UIView 的 alpha,但我试图在单独的控制器中添加一个新的子视图。我有这个第二版工作,除了我无法访问新视图控制器中的 label.text 。 好的,应该很简单。我创建了一个测试并且它正在工作。要将信息传递给“详细视图”,您必须从详细视图中保留该对象,并在您从第一个视图初始化时传递它。要将信息“第二/第一”传回,您必须在第二个视图中创建一个委托,并从该委托的第一个视图中获取信息。希望这会有所帮助。 知道了。我不能感谢你!伟大的社区在这里提供帮助。【参考方案2】:

行内:

self.textView.text = [vc1Controller self.label.text];

您在 [vc1Controller self.label.text] 中使用了 self 关键字,但这将引用当前视图控制器(视图控制器 2)中不存在的 UILabel。将vc1Controller中的label设为属性,然后使用:

vc1Controller.label.text;

访问其文本值。

【讨论】:

我更改了原始帖子以符合您的建议。我确实尝试过这个。我没有收到任何编译错误,但由于某种原因我没有访问文本值。我得到一个 NULL 输出。【参考方案3】:

当你在 VC2 中创建一个 VC1 对象时,你显然不是指推送 VC2 的 VC1。

您的 vc2 正在尝试从新创建的 vc1 中读取标签值,而您实际上想从推送 VC2 的 VC1 中读取值。

有几种方法可以让您的代码正常工作:

1) 访问 parentViewController(如果您将 vc2 呈现为模态)

在VC2中,你可以参考你的父控制器:

VC1* vc = (VC1 *)self.parentViewController; VC2.textView.text = vc.label.text;

2) 从导航控制器堆栈访问 vc1: How to access the stack in UINavigationController这个帖子什么都有

3) 使用委托模式。 Custom delegate这一款应有尽有!

希望你觉得这很有用。

【讨论】:

试图从 didSelectCellAtIndex 将图片加载到新的 ViewController

】试图从didSelectCellAtIndex将图片加载到新的ViewController【英文标题】:tryingtoloadapictureintonewViewControllerfromdidSelectCellAtIndex【发布时间】:2015-07-1217:44:27【问题描述】:我有一个CollectionView,在didSelectItemAtIndexPath方法中我实例化了一... 查看详情

如何将 Firebase iOS 中的数据从 TableViewController 传递到另一个 ViewController

】如何将FirebaseiOS中的数据从TableViewController传递到另一个ViewController【英文标题】:HowpassdatafromFirebaseiOSfromaTableViewControllertoanotherViewController【发布时间】:2017-10-1716:24:10【问题描述】:我正在开发一个应用程序,当我试图通过在... 查看详情

Swift - 从 CollectionViewCell 中的 UIButton 推送到 ViewController

】Swift-从CollectionViewCell中的UIButton推送到ViewController【英文标题】:Swift-PushtoViewControllerfromUIButtoninCollectionViewCell【发布时间】:2019-04-1323:12:22【问题描述】:我试图让我的按钮在被点击时推送到一个新的视图控制器。我尝试了许... 查看详情

我如何从我的 AppDelegate 访问另一个 ViewController 中的 Tableview 中的一个 ViewController?

】我如何从我的AppDelegate访问另一个ViewController中的Tableview中的一个ViewController?【英文标题】:HowcaniaccessaViewControllerthat\'saseguefromaTableviewinanotherViewControllerfrommyAppDelegate?【发布时间】:2014-06-1918:47:31【问题描述】:我是CoreData的... 查看详情

需要从另一个viewController调用其他viewController中的方法

】需要从另一个viewController调用其他viewController中的方法【英文标题】:NeedtocallmethodsinotherviewControllersfromanotherviewController【发布时间】:2013-01-0222:34:44【问题描述】:我有一个应用程序,它有多个viewController,其中一些viewController... 查看详情

从 Swift 中的其他类调用方法

...-09-0622:07:14【问题描述】:我有2个视图控制器类。我想从ViewController调用ViewController2的一个方法,但是控制台给了我这个错误:致命错误:在展开可选值时意外发现nil(lldb)这是我在ViewController类中的方法:classViewController:U 查看详情

多线程 ViewController 中的 UIWebView

】多线程ViewController中的UIWebView【英文标题】:UIWebViewinmultithreadViewController【发布时间】:2010-10-3100:18:46【问题描述】:我在视图控制器中有一个UIWebView,它有以下两种方法。问题是如果我在第二个线程完成之前弹出(点击导航... 查看详情

iOS - 使用 xib 中的 tabBarController 将数据从一个 viewController 传递到另一个 viewController

】iOS-使用xib中的tabBarController将数据从一个viewController传递到另一个viewController【英文标题】:iOS-PassDatafromoneviewControllertoanotherusingtabBarControllerinxib【发布时间】:2015-03-3118:57:19【问题描述】:我有一个要求,我必须在单击按钮时... 查看详情

如何从另一个类访问 ViewController 中的 IBOutlet?

】如何从另一个类访问ViewController中的IBOutlet?【英文标题】:HowcanIaccessanIBOutletinViewControllerfromanotherclass?【发布时间】:2015-12-2405:46:41【问题描述】:我有一个班级是GCDAsyncUdpSocketDelegate。现在我正在通过UDP(在后台线程中)获... 查看详情

从 Swift 中的不同类访问 ViewController 中的对象

】从Swift中的不同类访问ViewController中的对象【英文标题】:AccessobjectsinViewControllerfromadifferentclassinSwift【发布时间】:2016-02-1016:01:21【问题描述】:我有一个ViewController,其中有一个名为myView的UIView和一个名为myImageView的UIImageView... 查看详情

如何将数据从 ViewController 传递到标签栏 iOS 中的 ViewController?

】如何将数据从ViewController传递到标签栏iOS中的ViewController?【英文标题】:HowtopassdatafromViewControllertoaViewControllerinatabbariOS?【发布时间】:2017-11-0501:01:05【问题描述】:我正在尝试将数据从我的主ViewController传递到选项卡栏中的... 查看详情

试图显示从一个视图控制器捕获的 UIImage,并在 iOS 中的另一个视图控制器中显示它

...器中显示它【英文标题】:TryingtodisplayaUIImagecapturedfromoneviewController,anddisplayitinanotheriniOS【发布时间】:2013-08-2715:41:44【问题描述】:我有一个我正在开发的iPad应用程序,我从我的主viewController(称为mainViewController 查看详情

将数据从一个 ViewController 中的 UITextField 传递到另一个 View Controller 中的 UILabel

】将数据从一个ViewController中的UITextField传递到另一个ViewController中的UILabel【英文标题】:PassingDatafromaUITextFieldinOneViewControllertoaUILabelinanotherViewController【发布时间】:2013-09-2723:44:20【问题描述】:我被一些看起来相当简单的事情... 查看详情

如何将数据从模态呈现的 ViewController 传递到 Xamarin.iOS 中的另一个 ViewController?

】如何将数据从模态呈现的ViewController传递到Xamarin.iOS中的另一个ViewController?【英文标题】:HowtopassthedatafrommodallypresentedViewControllertoanotherViewControllerinXamarin.iOS?【发布时间】:2018-08-0310:36:48【问题描述】:我必须控制A和B。我从A... 查看详情

如何将 CoreLocation 值从一个 VIewController 传递到 iOS 中的 AppDelegate?

】如何将CoreLocation值从一个VIewController传递到iOS中的AppDelegate?【英文标题】:howtopassCoreLocationvaluesfromoneVIewControllertoAppDelegateiniOS?【发布时间】:2013-11-2013:13:54【问题描述】:我能够在一个ViewController中获取纬度和经度值。现在... 查看详情

如何从 SWIFT 3 中的另一个 ViewController 检查 UISwitch 的状态?

】如何从SWIFT3中的另一个ViewController检查UISwitch的状态?【英文标题】:HowtocheckthestateofanUISwitchfromanotherViewControllerinSWIFT3?【发布时间】:2016-08-0706:51:40【问题描述】:我想从另一个ViewController检查UISwitch的状态。如果switch是on,那... 查看详情

从 xib 文件中的操作按钮推送另一个 Viewcontroller

】从xib文件中的操作按钮推送另一个Viewcontroller【英文标题】:PushanotherViewcontrollerfromactionbuttoninxibfile【发布时间】:2016-10-1411:43:54【问题描述】:我有一个来自.xib的视图控制器。.xib里面有一个按钮;我希望按钮将视图控制器推... 查看详情

Storyboard - viewcontroller 中的导航控制器

】Storyboard-viewcontroller中的导航控制器【英文标题】:Storyboard-navigationcontrollerinviewcontroller【发布时间】:2012-07-1018:24:28【问题描述】:我是故事板的新手,在我的故事板中,我在视图控制器上有一个按钮。单击按钮时,我试图将... 查看详情