Objective c 将数据从ViewController传递到UIView
可能重复:Objective c 将数据从ViewController传递到UIView,objective-c,ios,uiview,uiviewcontroller,Objective C,Ios,Uiview,Uiviewcontroller,可能重复: 我在FirstViewController上有一个带有浮点值的数组。 当用户单击按钮时,我需要将此数组传递给我的视图。 此按钮将调用视图并绘制通知的点。1。 在FirstViewController.h中写入 #include <UIKit/UIKit.h> extern NSArray *yourArray; 现在,您必须在“MyView”类中导入整个FirstViewController。 简单地 #导入“FirstViewController.h” 在MyVie
我在FirstViewController上有一个带有浮点值的数组。 当用户单击按钮时,我需要将此数组传递给我的视图。 此按钮将调用视图并绘制通知的点。1。 在FirstViewController.h中写入
#include <UIKit/UIKit.h>
extern NSArray *yourArray;
现在,您必须在“MyView”类中导入整个FirstViewController。
简单地
#导入“FirstViewController.h”
在MyView.m。完成此操作后,“yourArray”将在MyView中可用
或者,2.,您可以在MyView.h中写入类似的内容:
- (void)drawRect(NSArray *)yourArray;
在FirstViewController.m中,只需传递数据:(不要忘记#包括MyView.h)
或者,3.,如果您使用的是故事板,您可以
- (IBAction)pushedButton(UIButton *)sender {
[self performSegueWithIdentifier:@"pushButton" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"pushButton"] {
MyView *destinationViewController = segue.destinationViewController;
destinationViewController.yourArray = yourArray; // you'll have to define yourArray in the .h File
}
}
谢谢。我在MyView上使用此阵列的时间浪费了2天。第一个答案很有效。
MyView *myView = [[MyView alloc]init];
[myView drawRect:yourArray]; //You'll have to pass "yourArray", to the function in MyView, which is going to be called
- (IBAction)pushedButton(UIButton *)sender {
[self performSegueWithIdentifier:@"pushButton" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"pushButton"] {
MyView *destinationViewController = segue.destinationViewController;
destinationViewController.yourArray = yourArray; // you'll have to define yourArray in the .h File
}
}