通过委派从类发送数据时出现问题

时间:2011-08-29 14:00:19

标签: iphone objective-c

我有一个问题,从其他人的一个类的值。基本上在我的项目中我有一个类placeTableView,它是表视图类。

in placesTableView.m
{
mapView *map=[[mapView alloc]init];
map.townName=@"london";

}


mapView.h - a class where delegate is defined and from this class i want to send data to     confirmController

@protocol mapViewDelegate; 

@interface mapView : UIViewController  { 


id <mapViewDelegate> delegate;// 
} 
@property (nonatomic, assign) id delegate;// 


@end 
@protocol mapViewDelegate <NSObject>// 

-(void)sendAStringToAnotherView:(NSString*)string; 
@end 

mapView.m

@synthesis townName;
-(void)viewDidLoad{ 


label.text=townName;//townName is getting value from previous view n showing here.


NSLog(@"%@",townName);//it shows value of townName so townName definetly contains value

//  NSString *a=[NSString stringWithFormat:@"%@",townName];

[delegate sendAStringToAnotherView:townName]; // this is sending method. i think problem is here
} 

confirmController.m - 发送数据的类

-(void)viewDidLoad{ 

mapView *myViewControllerPointer=[[mapView alloc] init]; 
myViewControllerPointer.delegate = self;// 

[self.view addSubview:self.myViewControllerPointer.view];

} 

-(void)sendAStringToAnotherView:(NSString*)string 
{ 
//displays the string as console output 
NSLog(@"lolo%@", [NSString stringWithFormat:@"%@",string]); // i want to show values here
} 

现在我想在mapView中显示confirmController中的townName。但它显示为null。但在mapView中,如果我使用字符串代替townName,则会在confirmController显示。

1 个答案:

答案 0 :(得分:0)

您必须在appDelegate中创建必要的变量,并在.h和.m文件中的Synthesize中设置其属性。现在,您可以在第一个类中为此变量设置值,并使用appDalegate变量

获取目标类的值