我试图在Swift中添加标签,这些标签是在循环中添加的.然后我想在添加时为每个添加一个'TapGesture'事件.它可以工作,但问题是被调用的函数从标签中获取数据,以便在单击时使用,但标签已经被重新定义,然后它会从最后添加的数据中获取数据,而不是单击的数据.我怎样才能使每一个都独一无二?
self.label.attributedText = self.myMutableString
let tapGesture = UITapGestureRecognizer(target: self, action: handleTap(label))
self.label.userInteractionEnabled=true
self.label.addGestureRecognizer(tapGesture)
self.label.font = UIFont.boldSystemFontOfSize(28)
self.label.sizeToFit()
self.label.center = CGPoint(x: screenWidth, y: top)
if(kilom>=30||self.located==false){
self.scroller.addSubview(self.label)
if(device=="iPhone"||device=="iPhone Simulator"){
top = top+80
}
else{
top = top+140
}
}
Run Code Online (Sandbox Code Playgroud)
下面的代码是手势识别器,它获取标签数据并使用它:
func handleTap(sender:UILabel){
var a = self.label.text
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("displayer")
self.presentViewController(resultViewController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我在 Android Studio 的资产文件目录中可能有 wifi2.txt 文件。但是,当我尝试访问它时,我不断收到 NULLPointException。我的代码如下:(提前非常感谢)
//CSV FILE READING
File file = null;
try {
FileInputStream is = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("wifi2.txt")));
String line;
Log.e("Reader Stuff",reader.readLine());
while ((line = reader.readLine()) != null) {
Log.e("code",line);
String[] RowData = line.split(",");
LatLng centerXY = new LatLng(Double.valueOf(RowData[1]), Double.valueOf(RowData[2]));
if (RowData.length == 4) {
mMap.addMarker(new MarkerOptions().position(centerXY).title(String.valueOf(RowData[0]) + String.valueOf(RowData[3])).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)