【发布时间】:2010-10-12 11:32:58
【问题描述】:
很抱歉标题非常模糊,我是 Objective C 的新手,并且在使用它时遇到了一些困难。基本上我有以下代码部分:
Graph *graph1 = [[Graph alloc] init];
[graph1 addNode:@"TEST"];
这在一定程度上起作用。但我想更改它,因为上面的代码发生在按下按钮时,因此我假设我每次执行此操作时都会创建一个新的“*graph1”。我想我可以简单地把它改成这样:
if(self = [super init])
{
[self setGraph: [[Graph alloc] init]];
}
return self;
上面在init方法中,下面是修改后的函数:
[graph addNode:@"TEST"];
但是在调试时我发现 addNode 方法永远不会像这样被调用。
谢谢 扎克
这是 testViewController.h
#import <UIKit/UIKit.h>
@class Graph;
@class Node;
@interface testViewController : UIViewController {
Graph *graph;
UILabel *label;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) Graph *graph;
- (IBAction) buttonPressed:(id)sender;
@end
这是 textViewController.m
#import "testViewController.h"
#import "Graph.h"
@implementation testViewController
@synthesize label, graph;
- (id)init
{
if(self = [super init])
{
[self setGraph: [[Graph alloc] init]];
}
return self;
}
- (IBAction)buttonPressed:(id)sender
{
//Graph *graph1 = [[Graph alloc] init];
[graph addNode:@"TEST"];
Node *node1 = [[Node alloc] initWithLabel: @"LABEL"];
label.text = node1.label;
}
【问题讨论】:
-
只是为了清楚我已经合成了“图表”。
标签: iphone objective-c xcode ios