![](/img/trans.png)
[英]How to navigate to a ViewController from NSObject Class in iOS?
[英]iOS accessing NSObject class from my viewController
我正在尝试从我的viewcontoller访问另一个类,但是不起作用:
viewcontroller.h
#import <UIKit/UIKit.h>
@class firstClass; //nsobject class
@interface ViewController : UIViewController
{
firstClass *firstclass;
}
@property (retain,nonatomic) LEMZfirstClass *firstclass;
---
firstClass.h:
#import "LEMZViewController.h"
@interface firstClass : NSObject
{
ViewController *viewController;
}
@property (retain,nonatomic) ViewController *viewController;
-(void)doSomenthing;
firstClass.m:
@synthesize viewController;
-(void)doSomenthing
{
viewController.firstclass=self;
viewController.outPutLabel.text=@"This is my Label";
}
viewcontroller.m:
@synthesize firstclass;
- (void)viewDidLoad
{
[super viewDidLoad];
[firstclass doSomenthing];
}
它可以编译,没有错误,但是标签永远不会更新,因此,第一类永远不会全部调用。 我做错了什么? 非常感谢您的帮助。
我要注意的几件事:
必须先创建它,然后才能在firstclass上调用某些东西。 像这样:
firstclass = [[firstClass alloc] init]; [一流的doSomenthing];
viewController.firstclass=self;
那行将是多余的。
您的firstClass.h
#import <Foundation/Foundation.h>
@interface firstClass : NSObject
+(NSString *)doSomenthing; //Instance Class
@end
firstClass.m
#import "firstClass.h"
@implementation firstClass
+(NSString *)doSomenthing
{
return @"This is my Label";
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "firstClass.h"
@interface ViewController : UIViewController
@end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[firstClass doSomenthing];
outPutLabel.text=[firstClass doSomenthing];;
// Do any additional setup after loading the view, typically from a nib.
}
注意:这里我使用实例类。 在使用此代码之前,您必须研究实例类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.