我想用自定義單元格中的按鈕顯示警報。我如何在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. 顯示來自應用程序委託的警報,然後顯示來自viewDidload的警報
- 3. Objective c UITableViewCell不顯示內容
- 4. Objective-C警報..不工作?
- 5. 以中間值顯示警報(Objective-C或任何其他)
- 6. 來自java類的調用JavaScript警報
- 7. 自定義警報內的警報在android中顯示錯誤?
- 8. 顯示類似於警報的網格
- 9. 顯示來自Swift腳本的系統警報或通知
- 10. 顯示來自Global.asax的警報消息框(在Application_Error事件中)
- 11. 顯示UI元素,如來自多個線程的警報
- 12. 如何顯示來自PHP的JavaScript警報?
- 13. Android顯示警報
- 14. 不顯示警報
- 15. 來自MongoDB的警報
- 16. android可顯示來自非UI或非活動類的警報對話框
- 17. 的JavaScript不顯示警報
- 18. onActivityResult中顯示的警報
- 19. 如何顯示來自plist的標籤上的值。 Objective-C的
- 20. 從自定義助手類顯示警報對話框
- 21. 動態顯示來自服務的Objective-C
- 22. 顯示來自控制器在asp.net mvc中的動作的javascript警報mvc
- 23. NSAlert beginSheetModalForWindow不顯示警報
- 24. 顯示數據表警報
- 25. 在Activity.onCreate中顯示警報(..)
- 26. Bootstrap警報不會顯示
- 27. 在clickedButtonAtIndex中顯示警報?
- 28. OnBackPressed不顯示警報
- 29. JavaFX警報不顯示?
- 30. UIWebView中顯示警報
你試過用谷歌搜索嗎? 「objective-c button tableview」提供了很多可能有用的結果。 – Ralfonso
我知道如何添加按鈕,但我不能顯示警報 – user2254968
噢好的。那麼,這是一個幫助您使用StackOverflow的機會 - 然後 - 獲得更好的響應,嘗試儘可能地詢問具體問題,併發布一些代碼。口頭禪是,當你試圖改進你的問題時,你通常會自己找到解決方案。你是否專門搜索顯示按鈕按下的警報?使用谷歌搜索「UIButton的UIAlert」可能會提供一個解決方案。 – Ralfonso