Iphone 更改独立于主ViewController类的.xib中的标签文本
我拥有的是在单独的Iphone 更改独立于主ViewController类的.xib中的标签文本,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我拥有的是在单独的.xib中创建的自定义表格单元格。有了它,我就有了一个Objective-C类。我将自定义单元格中的标签连接到自定义单元格的类 在main.xib中,我有一个自定义单元格添加到的表视图。进行填充的代码位于主类(ViewController.m)中 那么,如何从主类(ViewController.m)更改自定义单元格中的标签呢 当用户点击自定义单元格时,将显示一个对话框,自定义单元格中标签的文本将根据对话框中选择的按钮进行更改。您必须为标签指定一个标记,即在界面生成器中指定99。
.xib
中创建的自定义表格单元格。有了它,我就有了一个Objective-C类。我将自定义单元格中的标签连接到自定义单元格的类
在main.xib中,我有一个自定义单元格添加到的表视图。进行填充的代码位于主类(ViewController.m)中
那么,如何从主类(ViewController.m)更改自定义单元格中的标签呢
当用户点击自定义单元格时,将显示一个对话框,自定义单元格中标签的文本将根据对话框中选择的按钮进行更改。您必须为标签指定一个标记,即在界面生成器中指定99。
然后,在ViewController.m中,当您加载单元格时,在加载xib之后,您可以执行以下操作
UILabel *label = [cell viewWithTag: 99];
label.text = @"Some text here... (:";
这样就可以了!:) 您必须为标签指定一个标记,即界面生成器中的99。
然后,在ViewController.m中,当您加载单元格时,在加载xib之后,您可以执行以下操作
UILabel *label = [cell viewWithTag: 99];
label.text = @"Some text here... (:";
这样就可以了!:) 因为它是一个表单元格,所以我假设您在表视图中使用它。你通常是通过
- (UITableViewCell *)UITableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCustomCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
for (id anObject in nib) {
if ([anObject isKindOfClass:[MyCustomCell class]]) {
cell = (MyCustomCell *)anObject;
}
}
}
cell.myLabel.text = @"Some Text"; // This will set myLabel text to "Some Text"
return cell;
}
因为它是一个表单元格,所以我假设您在表视图中使用它。你通常是通过
- (UITableViewCell *)UITableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myCustomCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
for (id anObject in nib) {
if ([anObject isKindOfClass:[MyCustomCell class]]) {
cell = (MyCustomCell *)anObject;
}
}
}
cell.myLabel.text = @"Some Text"; // This will set myLabel text to "Some Text"
return cell;
}
这很简单:
首先在customcell中创建标签的属性
Customcell.h
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *yourLabel;
现在在TableViewController中创建customcell的实例
YourTableViewController.h
@interface YourTableViewController : UITableViewController<
{
CustomCell *cell;
}
现在,如果您想用其他方法更新customcell的标签,而不仅仅是这样做
-(void)someMethod()
{
CustomCell *acell = (CustomCell *)[tableView cellForRowAtIndexPath:n];
acell.yourLabel.text = @"whatever you want to add.";
}
这很简单:
首先在customcell中创建标签的属性
Customcell.h
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *yourLabel;
现在在TableViewController中创建customcell的实例
YourTableViewController.h
@interface YourTableViewController : UITableViewController<
{
CustomCell *cell;
}
现在,如果您想用其他方法更新customcell的标签,而不仅仅是这样做
-(void)someMethod()
{
CustomCell *acell = (CustomCell *)[tableView cellForRowAtIndexPath:n];
acell.yourLabel.text = @"whatever you want to add.";
}
我想出了一个不同的方法,现在我已经停止了iOS开发,因为我讨厌它,哈哈
无论如何,感谢大家这么快的回复。我想出了一个不同的方法,现在我已经停止了iOS开发,因为我讨厌它,哈哈
无论如何,谢谢你们这么快的回复