从另一个类

时间:2015-08-11 18:10:46

标签: ios objective-c xcode xcode6

我正在编写一个道路安全应用程序,并且会有很多代码,所以我认为我会将每个函数划分为一个类。

现在我正在尝试设置主ViewController的背景从另一个类,但它不起作用。这是我试过的代码:

-(void)StartBackgroundAnimation
{
ViewController *MainRoot = [[ViewController alloc] init];
MainRoot.BackgroundViewer.image = [UIImage imageNamed:@"Lunch_Background.png"];
NSLog(@"ok");
 }

我做的是创建了一个名为StartBackgroundAnimation的函数,然后导入了主视图控制器类,并从中创建了一个对象。之后,我将这个类导入到主视图控制器中,我调用了这个函数,但图像不起作用。

注意:NSLog完美无缺,并调用该函数,但图像不会改变。

2 个答案:

答案 0 :(得分:0)

如果你真的想在不同的类中存储函数,只需返回一个UIImage并在视图控制器中分配它。

所以在你的自定义类中。

- (UIImage*)backgroundImage{
 return [UIImage imageNamed:@"Lunch_Background.png"];
}

主视图控制器内部:

self.view.backgroundView.image = [instanceofyourclass backgroundImage];

答案 1 :(得分:0)

如果您想要从班级以外的班级访问数据,通常最好/usr/local/使用某个媒体资源。

Objective-C

这样,您可以在课程中使用// MyClass.h @interface MyClass : Superclass @property (strong, nonatomic) UIImage * backgroundImage; @end 并从课程外部使用self.backgroundImage来读取/写入属性。

编辑:拼写...

相关问题