I have a tableView and i made its cells custom and added a UIButton to the cell
i want when i press the button to popOver a new ViewController and in it the text in the cell i clicked the button in it. I don't want to use didSelectRowAtIndexPath func, i want to use the event of the button in each cell.
I tried to connect the button with the new viewController using popOver segue but the build fails because it is dynamic.
Please help and make the answer in swift.
Thanks in advance
You shouls set target to button inside the cell in cellForRowAtIndexPath function
cell.button.addTarget(self, action: "showPopupPage:", forControlEvents: UIControlEvents.TouchUpInside)
create a function
func showPopupPage(sender: UIButton) {
let view = button.superview!
let cell = view.superview as! custom cell
// you can get cell contents here
// call your popupview pass the value
}
Example=>
Swift - UIPopoverController in iOS 8
Related
I try to make the slideshow using uiscrollview, it will be triggered by slide event from uiscrollview.
Everything works fine except I try to add uibutton to the view, the button can be added and shown in the view but when I try to click the button, it does not triggered the function that I have stated in the addTarget function.
Anyone knows how to triggered the function assigned to the button?
First Create func:
func messagePrint(_ sender:UIButton) {
print("function called")
}
Then addTarget into UIButton:
btnDemo.addTarget(self, action: #selector(messagePrint(_:)), for: .touchUpInside)
In Above code btnDemo is UIButton Outlet.
I have a UIButton in a prototype UItableviewcell that is made in a nib.
The UITableView is inside a UIViewController.
The requirement is: When I press the UIButton, a segue should be performed from Main UIViewController to another second UIViewController . I can access the UIButton IBOutlet from the table , so is there any way to call a function through that UIButton Variable.
// for reference
let x= cell.followButton;
Add a target to the button to call a method in your view controller. In this code, self is your view controller.
cell.followButton.addTarget(self, action: #selector(buttonPressed), forControlEvents: .TouchUpInside)
Then implement buttonPressed to handle segueing.
func buttonPressed() {
performSegueWithIdentifier("Identifier", sender: self)
}
There is a button in my custom tableviewCell, when I press the UIButton, it will open another view in my app.
Everything is okay after I try to active the auto layout attribute.
And even I don't set any constraint to my button, it still can not be
detected and shift to another view.
A snapshot image here
Can anyone help me ?
UPDATE:
Thanks for Korpel, rose and Tobonaut.
Yes I had create the segue on the button to another view, and it worked before I set the attribute of autoLayout. I was sure I added the segue and identifier both on the storyboard and the code .
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier=="RemindSegue" {
let destination = segue.destinationViewController as! RemindTimeController
let senderButton = sender as? UIButton
let indexPath = NSIndexPath(forRow: (senderButton?.tag)!, inSection: 1)
destination.delegate = self
}
}
After I set the autoLayout option, I could only click on my custom tableCell, which was dynamically created on the runtime. And the click on the button didn't work anymore.
The button's still there, there was no constraint added to it.
Ok, I find the answer of it.
I added the following code in my custom tableViewCell when initiate...
which disabled my button to use.
But I don't really understand the reason behind it...
self.contentView.translatesAutoresizingMaskIntoConstraints = false
I have a tableview setup whereby every row has its own custom section header. This is done with a prototype cell in the story board. I have a segue to a view controller on the click event of a row. I achieved this by ctrl dragging on the storyboard.
I want my section header to segue to a different controller, however. I ctrl dragged from my header prototype cell to a view controller on the storyboard also.
The result is that both clicking on the row and clicking on the header of the row are bringing me to the same view controller.
It seems the header is linked to the row. Is there any way around this? Could I give my segues identifiers and override the prepare for seg? Or is it possible to have a separate click event on a section header as the rest of the row?
UPDATE:
I have to tried to implement it as follows:
I ctrl dragged from my vc to the destination vc in the storyboard and gave the seg an identifier.
In my viewForHeaderInSection, I have added the following:
let tapper = UITapGestureRecognizer(target: self, action:Selector("seg"))
tapper.cancelsTouchesInView = false
headerCell.addGestureRecognizer(tapper)
return headerCell
my seg func just perform a seg with the identifier I set up as explained above.
func seg(){
self.performSegueWithIdentifier("feedToMembersDetailed", sender: self)
}
In my overridePrepareForSeg, I check the identifier and call the appropriate method:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "feedToMembersDetailed"){
//getting in here
getMembersContentFromRow(segue)
}
... extra conditions
}
and finally my getMembersContentFromRow looks as follows:
func getMembersContentFromRow(segue : UIStoryboardSegue){
var membersDetailed = segue.destinationViewController as! MembersDetailedController
if let indexPath = self.feedTableView.**indexPathForSelectedRow()**{
if let selectedId = self.newsFeed?[indexPath.section].authorId{
Prefs.followedId = selectedId.toInt()!
}
println("setting postAuthorPic as \(self.newsFeed?[indexPath.section].authorPic)")
membersDetailed.postAuthorPic = self.newsFeed?[indexPath.section].authorPic
membersDetailed.postAuthorID = self.newsFeed?[indexPath.section].authorId
membersDetailed.postAuthor = self.newsFeed?[indexPath.section].author
}
else{
//getting in here. I need a method that can retrieve indexPath for me
println("could not get selected row")
}
}
Ok here it is, I am not writing code, just the steps you need to follow
Remove the segues you have created from your row and headers. Just assign SegueIdentifier for the controllers to be opened, say SegueIdentifierForRow and SegueIdentifierForSection.
Since there is no action in tableview headers, you need to add a custom action may be an UIButton or, UITapGestureRecognizer. Cell selection you can leave as it is. And to determine section selection you can set tag for each section.
On Row CellSelection manually initialise ViewController by using identifier SegueIdentifierForRow, and same for section using different identifier SegueIdentifierForSection.
Follow above you should be able to do whats required.
Cheers.
You would need to implement viewForHeaderInSection: method and add a UITapGestureRecognizer to a view which would be retuned as header view. You can then handle the tap on your table section headers to go to different View Controller.
I want to create a segue to from an object (label) inside a tableview cell to another view controller.
example: imagine the instagram feed tableview - tapping the number of likes under an image will push a second view with the list of people who liked an image.
How can i do that?
I know it's possible when tapping a cell but i need a solution for when tapping a label inside the cell...
thanks!
You have to make the following steps :
Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled.
Define an action to the Tap Gesture Recognizer to handle the tap inside the label.
To present the another ViewController you can do one of the following two things :
Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function.
I thought the first is more easy to present the another ViewController, Is up to you.
I hope this help you.
You can simply use a button and set its title instead of using a label. If you have to use a UILabel for some reason, then you can do so by setting its userInteractionEnabled property to true and then adding a UITapGestureRecognizer to it.
You can use UITapGestureRecognizer.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblTxt: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
recognizer.numberOfTapsRequired = 1;
lblTxt.addGestureRecognizer(recognizer)
lblTxt.userInteractionEnabled = true;
}
func handleTap(recognizer: UITapGestureRecognizer){
println("tapped!")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("secondView") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil)
}