我正在尝试将Android应用程序移植到ios。自从我完成了c / c ++以来,我已经有一段时间了,而且我对目标c完全不熟悉了:我开始使用单个视图“ViewController”。我有一个按钮,在ViewController.mm中创建数据。为简单起见,我们说它只是一个双倍。
ViewController.mm:
double myDouble = 13;
我有一个按钮,可以启动下一个视图:
[[NSBundle mainBundle] loadNibNamed:@"TrimView" owner:self options:nil];
[self.view addSubview:TrimView];
在“TrimView”上我想以图形方式显示我在ViewController.mm中获得的数据,所以双重myDouble。 我按照这个小教程来绘制线条: http://www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D
我使用此代码绘制线条: TrimView.mm:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
我有点卡住了,我现在正在寻找意见,我该怎么做。为简单起见,我想只想启动在位置myDouble的trimView.mm中绘制的线。我如何将它从ViewController传输到trimView?
我现在的问题是我没有像
这样的东西TrimView tf;
在ViewController.mm中,我可以这样做:
TrimView.setMyDouble(myDouble);
我刚才(如上所述):
[[NSBundle mainBundle] loadNibNamed:@"TrimView" owner:self options:nil];
[self.view addSubview:TrimView];
阅读答案后,我试了一下:
TrimView *nextView = [[TrimView alloc] initWithNib=@"TrimView" bundle=nil];
但得到了这个:“'UIView'没有可见的@interface声明选择器'alloc'”
TrimView.h:
#import <UIKit/UIKit.h>
@interface TrimView: UIView
@property(nonatomic) double *myDouble;
@end
答案 0 :(得分:1)
示例,使用ViewControllerA和ViewControllerB。
要将ViewControllerA中的double
值传递给ViewControllerB,我们将执行以下操作:
1)在ViewControllerB.h中为BOOL创建一个属性
@property(nonatomic) double *myDouble;
2)在ViewControllerA中,包含ViewControllerB:
#import "ViewControllerB.h"
3)当你想加载视图时,你需要在将它推入导航堆栈之前在ViewControllerB中设置该属性:
ViewControllerB *nextView = [[ViewControllerB alloc] initWithNib=@"ViewControllerB" bundle=nil];
nextView.myDouble = myDouble;
[self pushViewController:nextView animated:YES];
这会将ViewControllerB中的double设置为您想要的值。
在您的情况下,您可以将最后一行更改为[self.view addSubview:nextView];
。
答案 1 :(得分:0)
在TrimView上创建双重属性 - 读取属性。将创建TrimView,然后通过设置所需的属性进行配置,并在运行时轻松获取这些属性。
@property (nonatomic, assign) double myDouble;