从swift为objective-c变量赋值

时间:2016-07-29 07:33:12

标签: ios objective-c swift

我想要做的是当用户从Swift中的UITableView中选择一个区域时,我正在使用区域名称并向其添加.png并将图像名称变量发送到Objective-C文件中在里面,我正在创建UIImageView

这是我在Objective-C .h文件中的代码,

@property (weak, nonatomic) NSString *mapImage;

这是我在用户点击UITableViewUITableView)时从Swift didSelectItemAtIndexPath为其指定值的方式,

selectesState = namesArray[indexPath.row]
let temp :String = selectesState + ".png"
let instanceOfCustomObject: StateMapPinView = StateMapPinView()
instanceOfCustomObject.mapImage = temp

这是我在.m文件中设置图像的Objective-C代码(ViewDidLoad),

UIImage *image = [UIImage imageNamed:_mapImage];
_myView = [[VIPhotoView alloc] initWithFrame:self.view.bounds andImage:image];
_myView.autoresizingMask = (1 << 6) -1;

[self.view addSubview:_myView];

但它不断返回空值 我的代码怎么了?

2 个答案:

答案 0 :(得分:2)

您必须将属性声明更改为:

@property (copy, nonatomic) NSString *mapImage;

这将确保在分配时复制值。在您的情况下,您使用weak,因此在当前范围的执行结束时释放该值。

答案 1 :(得分:0)

Makesure,你有相同的&#39; instanceOfCustomObject&#39;实例。 如果再次实例化,那么对于地图图像来说,你将获得null。

使用&#39; instanceOfCustomObject&#39;的相同实例并试试你的Obj-c。

setTimeout(function() {
    leafletMap.setZoom(2);       
}, 300);
...
setTimeout(function() {
    leafletMap.setZoom(7);       
}, 300);