[英]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.