Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-8及更高版本-UIAlertController内的UITableView_Ios_Objective C_Uitableview_Uialertcontroller - Fatal编程技术网

iOS-8及更高版本-UIAlertController内的UITableView

iOS-8及更高版本-UIAlertController内的UITableView,ios,objective-c,uitableview,uialertcontroller,Ios,Objective C,Uitableview,Uialertcontroller,我知道如何使用accessoryView像UITableView一样在UIAlertView中添加任何自定义UI,但我现在好奇的是,如果我们仍然可以选择在UIAlertController中添加自定义UI,我想要的是一个UIAlertController内部的UITableViewController,并且理解清楚 UIViewController *tempViewController = [[UIViewController alloc] init]; tempViewControl

我知道如何使用
accessoryView
UITableView
一样在
UIAlertView
中添加任何
自定义UI
,但我现在好奇的是,如果我们仍然可以选择在
UIAlertController
中添加
自定义UI
,我想要的是一个
UIAlertController
内部的
UITableViewController
,并且理解清楚

UIViewController *tempViewController = [[UIViewController alloc] init];
    tempViewController.view.backgroundColor = [UIColor redColor];

    [alertController setValue:tempViewController forKey:@"contentViewController"];

这段代码将在警报视图上显示一个红色视图,现在您可以轻松地将
UITableView
放在
UIViewController
中。Happy
UIAlertController
定制;)

感谢StackOverflow用户,我能够完成这项任务

这是我的密码:

UIViewController *controller = [[UIViewController alloc]init];
UITableView *alertTableView;
CGRect rect;
if (array.count < 4) {
    rect = CGRectMake(0, 0, 272, 100);
    [controller setPreferredContentSize:rect.size];

}
else if (array.count < 6){
    rect = CGRectMake(0, 0, 272, 150);
    [controller setPreferredContentSize:rect.size];
}
else if (array.count < 8){
    rect = CGRectMake(0, 0, 272, 200);
    [controller setPreferredContentSize:rect.size];

}
else {
    rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];
 }

alertTableView  = [[UITableView alloc]initWithFrame:rect];
alertTableView.delegate = self;
alertTableView.dataSource = self;
alertTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[alertTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[alertTableView setTag:kAlertTableViewTag];
[controller.view addSubview:alertTableView];
[controller.view bringSubviewToFront:alertTableView];
[controller.view setUserInteractionEnabled:YES];
[alertTableView setUserInteractionEnabled:YES];
[alertTableView setAllowsSelection:YES];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController setValue:controller forKey:@"contentViewController"];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
UIViewController*controller=[[UIViewController alloc]init];
UITableView*alertTableView;
CGRect rect;
if(array.count<4){
rect=CGRectMake(0,0272100);
[controller setPreferredContentSize:rect.size];
}
else if(array.count<6){
rect=CGRectMake(0,0272150);
[controller setPreferredContentSize:rect.size];
}
else if(array.count<8){
rect=CGRectMake(0,0272200);
[controller setPreferredContentSize:rect.size];
}
否则{
rect=CGRectMake(0,027250);
[controller setPreferredContentSize:rect.size];
}
alertTableView=[[UITableView alloc]initWithFrame:rect];
alertTableView.delegate=self;
alertTableView.dataSource=self;
alertTableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
[alertTableView设置分隔样式:UITableViewCellSeparatorStyleSingleLine];
[alertTableView设置标签:kAlertTableViewTag];
[controller.view addSubview:alertTableView];
[controller.view将子视图带到前台:alertTableView];
[controller.view setUserInteractionEnabled:是];
[alertTableView setUserInteractionEnabled:是];
[alertTableView SetAllowSelection:是];
UIAlertController*alertController=[UIAlertController alertControllerWithTitle:@“Title”消息:@“message”首选样式:UIAlertControllerStyleAlert];
[alertController设置值:控制器分叉:@“contentViewController”];
UIAlertAction*cancelAction=[UIAlertAction actionWithTitle:@“Cancel”样式:UIAlertActionStyleDestructive handler:^(UIAlertAction*action){
}];
[alertController添加操作:取消操作];
[self-presentViewController:alertController动画:是完成:无];
以下是Swift的简化格式:

let alertController = UIAlertController(title: "The Title",
                                        message: "Here's a message.",
                                        preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel)
{ (action) in
    // ...
}
alertController.addAction(cancelAction)

let okAction = UIAlertAction(title: "OK", style: .Default)
{ (action) in
    // ...
}
alertController.addAction(okAction)

let tableViewController = UITableViewController()
tableViewController.preferredContentSize = CGSize(width: 272, height: 176) // 4 default cell heights.
alertController.setValue(tableViewController, forKey: "contentViewController")

yourTopViewController().presentViewController(alertController, animated: true)
{
    // ...
}

以下是Swift 5示例代码:

    //MARK: - Properties
    private var alertController = UIAlertController()
    private var tblView = UITableView()

    //MARK: - TableViewAlert
    private func setupTableViewAlert() {
    
    let alertVC = UIViewController.init()
    let rect = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
    alertVC.preferredContentSize = rect.size
    
    tblView = UITableView(frame: rect)
    tblView.delegate = self;
    tblView.dataSource = self;
    tblView.tableFooterView = UIView(frame: .zero)
    tblView.separatorStyle = .singleLine
    alertVC.view.addSubview(tblView)
    alertVC.view.bringSubviewToFront(tblView)
    alertVC.view.isUserInteractionEnabled = true
    tblView.isUserInteractionEnabled = true
    tblView.allowsSelection = true
    
    self.alertController = UIAlertController(title: "Select City", message: nil, preferredStyle: .alert)

    //this is the main part
    //add local alert content over global one
    alertController.setValue(alertVC, forKey: "contentViewController")
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true, completion: nil)
}


extension SignupViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
    cell.textLabel?.text = "Cell \(indexPath.row + 1)"
    cell.textLabel?.textAlignment = .center
    cell.detailTextLabel?.textColor = .black
    return cell
}}

我在iOS 7中试过,而不是在iOS 8中试过你在
UIAlertController中试过吗?
UIAlertController中没有这样的选项是的,理论上这应该是可行的,当你完成时给我们看一个性感的屏幕截图^^@Zil检查我下面的答案。干,取
alertTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,272,xxx)
脱离if语句,并替换为
alertTableView=[[UITableView alloc]initWithFrame:rect];
谢谢:)很好的回答我发现了它UIAlertController的Alerts的宽度这太棒了,救了我一天!非常感谢!我知道你是一个新的贡献者,谢谢你的回答。你能用一些关于解决方案的细节来增强它吗?@Maheshviraktamath请检查这是好的,如果你需要编辑,请做我的嘉宾。这是个糟糕的建议。引用Apple的文档:
UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
@Gereon如果您有更好的解决方案,请与我们共享。