Presenting UIAlertController from UITableViewCell
You can use this extension to find the viewController that present the cell
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.nextResponder()
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
Or use rootViewController
to Present:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(refreshAlert, animated: true, completion: nil)
Swift 4.2 Update
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as? UIViewController
}
}
return nil
}
}
Or use rootViewController
to Present:
UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
Present UIAlertController in UITableViewCell on Presented View Controller
You need to implement the delegate
pattern.
Create a protocol
implemented by your controller and assign a weak
reference from this controller to your cell.
then use something like cell.delegate.didPerformAction()
The cell is a View
, and it should not be responsible for presenting anything.
Calling UIAlertController inside a UITableViewCell
You can try to use delegate
protocol AlertShower{
func showAlert(TableCustomCell)
}
class TableCustomCell: UITableViewCell {
var delegate: AlertShower?
@IBAction func showClicked(_ sender: UIButton) {
self.delegate?.alertShower(sender:self)
}
}
in the VC
class viewController: UIViewController, AlertShower {
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = areaSettTable.dequeueReusableCell(withIdentifier:CellIdentifier1) as! TableCustomCell
cell.delegate = self
return cell
}
func showAlert(sender:TableCustomCell) {
// show alert here
}
}
Show an UIAlertController from a button on an UITableViewCell
It crashed because of this line
shareAlert.presentViewController(shareAlert, animated: true, completion: nil)
A controller cannot present itself, so
Replace
shareAlert.presentViewController(shareAlert, ...)
by
yourCurrentViewController.presentViewController(shareAlert, ...)
And you should not use UIAlertView because it was deprecated in iOS 9 and maybe obsoleted in the next iOS, so use UIAlertController and you don't have to change it in near future
how to show UIAlertController in a custom UITableViewCell in button click using swift code?
i found a solution. following are code for above question.
write the following code for button in viewcontroller where table view are present.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let deerNameCell: CustomCellDeerCalls = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCellDeerCalls
let deercallcell=arrayOfCallsName[indexPath.row]
deerNameCell.DeerCallNameLabel.text=deercallcell.callName
deerNameCell.playButton.tag=indexPath.row
deerNameCell.detialsInfoButton.tag=indexPath.row
deerNameCell.detialsInfoButton.addTarget(self, action: "showAlert:", forControlEvents:UIControlEvents.TouchUpInside)
return deerNameCell
}
and the funtion for alert in the same view controller:
func showAlert(sender:UIButton!)
{
println(sender.tag)
let deercallcell=arrayOfCallsName[sender.tag]
var alert = UIAlertController(title: deercallcell.callName, message: arrayOfDetialsInfoDeerCalls[sender.tag], preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
peresent UIAlertController from UITableViewCell
In "tableViewCell.swift," put:
weak var delegate:yourViewController!
and then you have access to all variables from viewController
and all features, like:
present(refreshAlert, animated: true, completion: nil)
And, in "cellForRowAt indexPath," put:
cell.delegate = self
iOS - UITableViewCell, UIAlertController
What you are designing for implementing this procedure is not correct. What you can do
- Make a custom cell
- Add a button in custom cell
- Add action in that button in CellForRowAtIndexPath
- Handle action from ViewController where you added tableView.
I made a whole project for you.Just to let you know. if you want to add customCell in tableView you need to register it like this in viewDidLoad.
i have done is in ViewController.swift file. check out my project.
let nib = UINib.init(nibName:String(describing: sampleTableViewCell.self) , bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "chatCell")
Then check cellForRowAtIndexPath function:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! sampleTableViewCell
cell.clickMeBtn.tag = indexPath.row
cell.clickMeBtn.addTarget(self, action: #selector(onButtonPressed(sender :)), for: .touchUpInside)
return cell
}
Button press function:
func onButtonPressed(sender:UIButton) {
let alert = UIAlertController.init(title:"Cell index is"+String(sender.tag), message: nil, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction.init(title: "ok", style: UIAlertActionStyle.default) { (UIAlertAction) in
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
Check only three files:
Github link
ViewController.swift
sampleTableViewCell.swift
sampleTableViewCell.xib**
Here is the output:
Related Topics
Alphabetical Sections in Table Table View in Swift
Not Receiving Any Push Notification in Iphone
Storyboard Navigation Controller and Tab Bar Controller
How to Use the Default iOS7 Uianimation Curve
Push/Pop View Controller with Navigation Bar from View Controller Without Navigation Bar
Swift Calculate Md5 Checksum for Large Files
Best Way to Save and Retrieve Uicolors to Core Data
How to Write Application Logs to File and Get Them
Swift: Natively Detect If App Has Crashed
Communicating With/Opening Containing App from Share Extension
Swift: Custom Camera Save Modified Metadata with Image
Obj-C Afnetworking 2.0 Post Request Does Not Work
Does Nsthread Create Autoreleasepool Automatically Now
iOS - Uisplitviewcontroller with Storyboard - Multiple Master Views and Multiple Detail Views