简体   繁体   English

如何从另一个UIViewController更改UIViewController的UILabel

[英]how to change a UILabel of a UIViewController from another UIViewController

i have a two UIViewController named FirstUIViewController and SecondUIViewController and i want to change the text of a UILabel on the SecondtUIViewController from the FirstUIViewController when i pressed the button from the FirstUIviewController. 我有两个名为FirstUIViewController和SecondUIViewController的UIViewController,当我从FirstUIviewController按下按钮时,我想从FirstUIViewController更改SecondtUIViewController上UILabel的文本。 i don't know how to do it i am new to objective-c and ios development. 我不知道该怎么做,我是Objective-C和ios开发的新手。

here is what i currently have 这是我目前拥有的

FirstUIViewController.h FirstUIViewController.h

#import <UIKit/UIKit.h>

@interface FirstUIViewController

IBAction Button1:(id)sender;
@end

FirstUIViewController.m FirstUIViewController.m

#import "MainViewController.h"

@implementation FirstUIViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewdidload {
//some codes here.
}

SecondUIViewController.h SecondUIViewController.h

#import <UIKit/UIKit.h>

@interface SecondUIViewController {
IBOutlet UILabel *label;
}

@end

SecondUIViewController.m SecondUIViewController.m

#import "SecondUIViewController.h"

@implementation FirstUIViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewdidload {
//some codes here.
}

You will need to declare a delegate protocol for your class. 您将需要为类声明一个委托协议。 An example of a delegate protocol and interface for class Foo might look like this: Foo的委托协议和接口的示例可能如下所示:

@class Foo;
@protocol FooDelegate <NSObject>
@optional
- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;
- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;
@end

@interface Foo : NSObject {
     NSString *bar;
     id <FooDelegate> delegate;
}

@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <FooDelegate> delegate;

- (void)changeLabelText;

@end

Don't forget to synthesize your properties in the @implementation . 不要忘记在@implementation综合属性。

The last step is for a Foo object to be instantiated in the class that conforms to FooDelegate , and for this Foo object to have its delegate property set: 最后一步是在符合FooDelegate的类中实例化Foo对象,并为此Foo对象设置其委托属性:

Foo *obj = [[Foo alloc] init];
[obj setDelegate:self];

Now, your class is prepared to receive messages from Foo objects that have their delegates set correctly. 现在,您的类已经准备好接收来自Foo对象的消息,这些对象的委托设置正确。

if you are not using storyboard you just have to create an instance of the second screen and then you can refer to the secondviewcontroller properties. 如果不使用情节提要,则只需创建第二个屏幕的实例,然后可以引用secondviewcontroller属性。

on the .h file of the first screen: creating an instance of the secondviewpage 在第一个屏幕的.h文件上:创建secondviewpage的实例

@property (nonatomic,retain)SecondViewController *detailcontroller;

on the .m file of the first view (when you want to load the page). 在第一个视图的.m文件中(要加载页面时)。

self.detailcontroller = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

self.detailcontroller.label

//str as some nsstring variable in the secondViewController // str作为secondViewController中的nsstring变量

now you cant access the "SecondViewController" from the first view by the dot notation 现在您无法通过点符号从第一个视图访问“ SecondViewController”

hope i helped you. 希望我能帮助你。

idan 伊丹

Easy method is store the property in the AppDelegate and set/get it there. 简单的方法是将属性存储在AppDelegate中,然后在此处设置/获取它。

In AppDelegate.h @property (nonatomic, strong) NSString *transferString; AppDelegate.h @property (nonatomic, strong) NSString *transferString; In AppDelegate.m @synthesize transferString AppDelegate.m @synthesize transferString

In both your first and second v iewcontrollers.m import "AppDelegate.h" create a property for it @property (nonatomic, strong) AppDelegate *appDelegate; 在您的第一个和第二个iewcontrollers.m导入"AppDelegate.h"为其创建一个属性iewcontrollers.m @property (nonatomic, strong) AppDelegate *appDelegate;

in the viewcontrollers viewDidLoad set it self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 在viewcontrollers viewDidLoad中将其设置为self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

then just get or set the string value in the appDelegate in your viewcontrollers 然后只需在您的viewcontrollers中的appDelegate中获取或设置字符串值

to set it: self.appDelegate.transferString = aString; 进行设置: self.appDelegate.transferString = aString; to get it: NSString *theString = self.appDelegate.transferString; 得到它: NSString *theString = self.appDelegate.transferString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个UIViewController中从UiViewController更改UILabel文本 - Change UILabel Text from UiViewController in another UIViewController 如何使用Notifications和Observer从另一个UIViewController隐藏UILabel - How to hide a UILabel from another UIViewController using Notifications and Observer 从另一个uiviewcontroller向UILabel分配一个字符串 - assigning a string to a UILabel from another uiviewcontroller 从另一个UIViewController推送UIViewController-如何? - Pushing a UIViewController from another UIViewController - How to? 从另一个UIViewController呈现UIViewController - Present UIViewController from another UIViewController 目标C-如何从另一个类(另一个UIViewController)更改图像 - Objective C - How to change image from another class (another UIViewController) iOS 7:如何从其他方法更改为另一个UIViewController? - iOS 7: How to change to another UIViewController from an others method? 如何从另一个UIViewController更改分段控件标题(设置) - How to change segmented control title from another UIViewController (Settings) 如何将UILabel与UIViewController的角对齐 - How to align a UILabel to a corner of a UIViewController 如何将评论从一个UIViewController发送到另一个UIViewController并将其存储在数组中? - How to send comments from a UIViewController to another UIViewController and to store them in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM