繁体   English   中英

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

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

我有两个名为FirstUIViewController和SecondUIViewController的UIViewController,当我从FirstUIviewController按下按钮时,我想从FirstUIViewController更改SecondtUIViewController上UILabel的文本。 我不知道该怎么做,我是Objective-C和ios开发的新手。

这是我目前拥有的

FirstUIViewController.h

#import <UIKit/UIKit.h>

@interface FirstUIViewController

IBAction Button1:(id)sender;
@end

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

#import <UIKit/UIKit.h>

@interface SecondUIViewController {
IBOutlet UILabel *label;
}

@end

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.
}

您将需要为类声明一个委托协议。 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

不要忘记在@implementation综合属性。

最后一步是在符合FooDelegate的类中实例化Foo对象,并为此Foo对象设置其委托属性:

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

现在,您的类已经准备好接收来自Foo对象的消息,这些对象的委托设置正确。

如果不使用情节提要,则只需创建第二个屏幕的实例,然后可以引用secondviewcontroller属性。

在第一个屏幕的.h文件上:创建secondviewpage的实例

@property (nonatomic,retain)SecondViewController *detailcontroller;

在第一个视图的.m文件中(要加载页面时)。

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

self.detailcontroller.label

// str作为secondViewController中的nsstring变量

现在您无法通过点符号从第一个视图访问“ SecondViewController”

希望我能帮助你。

伊丹

简单的方法是将属性存储在AppDelegate中,然后在此处设置/获取它。

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

在您的第一个和第二个iewcontrollers.m导入"AppDelegate.h"为其创建一个属性iewcontrollers.m @property (nonatomic, strong) AppDelegate *appDelegate;

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

然后只需在您的viewcontrollers中的appDelegate中获取或设置字符串值

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM