我想用自定义单元格中的按钮显示警报。我如何在Objective C中做到这一点? 由于显示来自UITableViewCell类的警报 - Objective c
2
A
回答
0
传递的容器View Controller
一个weak
实例的定制表格视图细胞。
手机将使用此传入View Controller
显示UIAlertController
。
一些示例代码来实现,这将是这样的:
// CustomTableViewCell.h
@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, weak) __kindof UIViewController *controllerDelegate; // __kindof used to avoid importing the View Controller class, we only need a controller object & nothing else, hence using __kindof will suffice
@end
// CustomTableViewCell.m
@implementation CustomTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
...
}
return self;
}
- (void)setControllerDelegate:(__kindof UIViewController *)controllerDelegate{
_controllerDelegate = controllerDelegate;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is an alert!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelButton];
UIAlertAction *proceedButton = [UIAlertAction actionWithTitle:@"Proceed" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
... // add custom actions on alert button tap here
}];
[alertController addAction:proceedButton];
[_controllerDelegate presentViewController:alertController animated:YES completion:nil];
}
@end
// MyViewController.m
@interface MyViewController()
...
@end
@implementation MyViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:taskTableIdentifier forIndexPath:indexPath];
cell.controllerDelegate = self;
return cell;
}
@end
1
你只需要创建任何你想要在其中显示标签和按钮一个自定义单元格。
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@interface CustomTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) IBOutlet UIButton *btnAlert;
@end
CustomTableViewCell.h
#import "CustomTableViewCell.h"
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
现在,在你的控制器,你需要创建表视图,让您的自定义单元格和绑定按钮方法在cellForRowAtIndexPath。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static N
SString *simpleTableIdentifier = @"CustomTableViewCell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = (CustomTableViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lblName.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.btnAlert.tag = indexPath.row;
[cell.btnAlert addTarget:self action:@selector(alertButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(IBAction)alertButtonClicked:(id)sender
{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Button Clicked" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
+0
@ user2254968你有没有得到你的解决方案? – Nirmalsinh
相关问题
- 1. 显示来自URL的图像Objective C
- 2. 自举警报淡出类不显示
- 3. 显示来自应用程序委托的警报,然后显示来自viewDidload的警报
- 4. 如何显示来自非活动类的警报对话框
- 5. 自举警报不显示
- 6. Objective c UITableViewCell不显示内容
- 7. 在子类的类的新方法显示在Objective-C警告
- 8. Objective-C警报..不工作?
- 9. 以中间值显示警报(Objective-C或任何其他)
- 10. NSRunAlertPanel是否在任何空间显示警报? - Objective-C/10.7 +
- 11. 来自java类的调用JavaScript警报
- 12. 自定义警报内的警报在android中显示错误?
- 13. 如何显示来自阵列的所有消息的警报
- 14. 显示类似于警报的网格
- 15. AngularJS:警报显示不出来
- 16. 来自报警管理器的显示通知
- 17. 显示来自Global.asax的警报消息框(在Application_Error事件中)
- 18. 如何显示来自PHP的JavaScript警报?
- 19. 显示UI元素,如来自多个线程的警报
- 20. 显示来自Swift脚本的系统警报或通知
- 21. Android显示警报
- 22. jsp显示警报
- 23. 不显示警报
- 24. 不显示警报
- 25. 来自MongoDB的警报
- 26. android可显示来自非UI或非活动类的警报对话框
- 27. 关于更改显示警报C#asp.net
- 28. 的JavaScript不显示警报
- 29. UIImagePickerController显示的VIDEO_TOO_LONG_TITLE警报
- 30. onActivityResult中显示的警报
你试过用谷歌搜索吗? “objective-c button tableview”提供了很多可能有用的结果。 – Ralfonso
我知道如何添加按钮,但我不能显示警报 – user2254968
噢好的。那么,这是一个帮助您使用StackOverflow的机会 - 然后 - 获得更好的响应,尝试尽可能地询问具体问题,并发布一些代码。口头禅是,当你试图改进你的问题时,你通常会自己找到解决方案。你是否专门搜索显示按钮按下的警报?使用谷歌搜索“UIButton的UIAlert”可能会提供一个解决方案。 – Ralfonso