Swift - Can not detect button action in uitableviewcell after set auto layout - ios

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

Related

separate segue on section header as on table view row

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.

Swift - Hiding Tab bar in PageViewContoller scenes

I set up a series of onboarding scenes for my app using this tutorial, and elsewhere in my app use a TabViewController. When I run the app, the Onboarding scenes have a blank tab bar at the bottom.
I've tried ticking "Hide Bottom Bar on Push" from all 3 views on interface builder:
I've also tried adding
self.hidesBottomBarWhenPushed = true to the override func viewDidLoad() functions of the ViewController, PageViewController and PageContentViewController. No joy!
Also tried self.tabBarController?.tabBar.hidden = true in the same places.
Elsewhere I've found references to this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showLogin"{
println("showLogin seque called")
let bottomBar = segue.destinationViewController as LoginViewController
bottomBar.hidesBottomBarWhenPushed = true
bottomBar.navigationItem.hidesBackButton = true
}
}
but, I don't have any named segues, so not sure how to identify the segue.
Any help would be amazing!
Thanks
To give the Segue name, first choose the segue and then go to Attributes Inspector.
Below there you find Storyboard Segue, from there you provide Identifier name in Identifier Box.
Thanks

Different behavior for segue using UISearchController

I must be doing something wrong here.
I have a UITableView, and have implemented a UISearchController.
I have a Prototype cell linked to the details screen and pass the selected value in the prepareForSegue method.
For the normal view controller, all works OK, a row is selected and the details screen pushed (i.e. slide in from right).
However when there is an active search, using the UISearchController, the details screen is presented modally (i.e. slide up from bottom of screen) without the UINavigationBar (so there is no possibility to go "back")
I am not using didSelectRowAtIndexPath since I have used storyboard to push the details screen
Why is the presenting animation different when the same code in "prepareForSegue" is being correctly called in each case:
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier! == "PresentContactDetail") {
// pass person details from selected row
if let selectedRow = self.tableView.indexPathForSelectedRow()?.row {
let selectedPerson = self.visibleResults[selectedRow]
(segue.destinationViewController as ContactDetail).personRecord = selectedPerson
}
}
}
Any suggestions gratefully received.
in ViewDidLoad of UITableViewController set
definesPresentationContext = true

Accesing parent cell labels swift

I have a button placed inside a TableViewCell, the button triggers a action which passes data to another ViewController and displays it. Is there any way to access the labels of the particular cell that I clicked?
The cells are being created and populated from an API.
For example, I have 10 cells, all having different labels inside. I need to access the labels of the cell that I clicked (Or the cell that the button I clicked on is a child to) and pass this data to my other ViewController
Cheers!
I solved this by giving the button tag an ID from my API and passing this to the other ViewController.
That works, I had just figured this out:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let indexPath = self.tableView.indexPathForSelectedRow()
let cell = self.historyViewTable.cellForRowAtIndexPath(indexPath!)
println(cell.cellLabel.text!)
}

Change View with segue

I try to create my first iPhone application. I created two differents views in my Storyboard.
A "master" and a "detail" view. I embedded the Master View into a Navigation Controller. The Master View is made of an UITableView, with custom cells.
I try to show the Detail View when I tap the cell. but I don't understand how to make that... I created a "view detail" segue between the cell and the Detail View, but when I tap the cell, nothing happens.
Thanks for the help.
Embed the UITableView into a navigation controller.
Do you have a code in your method "didSelectRowAtIndexPath" in your table?
You can try with this solution here:
The problem is that you're not handling your data correctly. If you look into your currentResponse Array, you'll see that it holds NSDictionaries but in your prepareForSegue you try to cast a NSDictionary to a NSArray, which will make the app crash.
Change the data variable in RestaurantViewController to a NSDictionary and change your prepareForSegue to pass a a NSDictionary
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let cell = sender as? UITableViewCell {
let i = redditListTableView.indexPathForCell(cell)!.row
if segue.identifier == "toRestaurant" {
let vc = segue.destinationViewController as RestaurantViewController
vc.data = currentResponse[i] as NSDictionary
}
}
}

Resources