objective-c - 如何设置另一个变量/UILabel 变量的引用变量?

标签 objective-c cocoa cocoa-touch

假设我有几行代码来阐明给定 UILabel 变量的特定设置:

numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].text = @"derp";      
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.x = 5;
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.y = 3;

我想在 Obj-C 中为这个庞大的数组索引指定变量设置一个引用变量(PHP 中的 &$varname)。最好的方法是什么?

最佳答案

只需使用指针:

UILabel* label = numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY];
label.text = @"derp";
label.center.x = 5;
label.center.y = 3;

由于您没有写入数组,因此您不需要任何比这更奇特的东西。如果要覆盖数组中的值,则可以使用指向指针的指针:

UILabel** label_in_array = &numberMarkings[selectedBoxX]/* ... */[selectedNoteY];
// Write to the label
UILabel* label = *label_in_array;
label.text = @"derp";
// Write to the array
[label release];
*label_in_array = [[UILabel alloc] init]; // Now numberMarkings[][][...][]
                                          // holds a new uilabel object.

关于objective-c - 如何设置另一个变量/UILabel 变量的引用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3222054/

相关文章:

ios - Objective-C 项目中的 Swift CocoaPods 库

objective-c - 词法或预处理器问题 : 'MyViewController.h' file not Found

iphone - NSURLConnection : gzip encoded SOAP response gets corrupted

objective-c - 移动的球和线之间的碰撞

objective-c - 以编程方式更改 UIButton 类型

ios - SpriteKit 在暂停后不会恢复

iphone - 对 <UINavigationController : 0xa98e050> 的开始/结束外观转换的不平衡调用

macos - (Mac OS X,Cocoa)如何在输入文本字段中制作可拖放的占位符元素?

objective-c - 向 NSImageView 添加图像?

ios - 如何在 xcode 7、iOS swift 2 中截取 View 的部分屏幕截图?