I am using this open source framework in swift https://github.com/uacaps/PageMenu for my view controller.
I am using xib for the tableview cells and the tableview as shown in the demo projects.
However, when I go to connect my buttons from the tableview cell to the table view controller Xcode will not let me. I am working on the NotificationTableViewCell and NotificationTableVC files.
I have set up the project properly and can get the cell up and running as well as the project itself. However if I can't get the buttons to connect I cannot add the needed functionality.
The demo projects also have this behaviour when I cloned some from the repository. I looked through the issues and no one seemed to mention anything about connecting IBActions to the view, so I did, but I assume there is something I am missing.
I have attached screen shots to help you see my issue. I need to access the cellForRowIndexPath as well.
My xib is given the class of my table view cell which still wont allow me to interact with the tableview controller.
Okay, so I have figured out a working solution. Thanks to Gurtej Singh.
Here is the code:
1) I added this line in the cellForRowIndexPath
cell.mybutton.addTarget(self, action: "declineRequest:", forControlEvents: .TouchUpInside)
2) Now I am able to access the button if I write a function called declineRequest
Since I want to delete the cell based on its data I use this function and add the Parse query logic.
func declineRequest(sender: UIButton) {
var cell: NotificationsTableViewCell = sender.superview!.superview as! NotificationsTableViewCell
.... }
I don't think you will be able to connect a table view cell element to a table view controller class via IB (and from what I know, it's not correct to do so as well, because there will be multiple cells and re-usability).
Connect the element from the IB to your Cell class (.h/.m), create one if you don't have it , like NotificationTableViewCell.m, and then access the connected element from the delegate methods inside your Table View controller class.
Hope this helps.
Related
While writing an app in Xcode8 (swift3) for adding pictures with titles I set up two view controllers, the first of which was embedded in navigation controller. I’ve added an “Add” barbutton in the navigation control of the first view controller (VC), tapping which, the second view controller is shown. I receive the following error when I tap on the “Add” button on the 1st VC.
"objc[692]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x155d38cc0) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x154b876f0). One of the two will be used. Which one is undefined."
Can somebody please help me understand the issue and to solve it.
I'm using Storyboards for the first time creating a master/detail view for a simple object.
Both view controller items of the split view have their own view & NSObjectControllers. The controller for the table view is a NSArrayController (which shows all instances of the Entity). The controller for the detail view is an NSObjectController, which shows just the selected instance.
When I click an object in the table, I'd like the detail to show up in the detail view. So I believe my goal here is to bind the 'contents' of the detail object controller to the 'selection' of the master array controller. However with storyboards I cannot cross view controller boundaries (can't create the IB 'link'. aka: can't bind cross VC).
Whats the best practice for doing this?
So far all I can think to do is manually set all this up in one of the controllers (either the root controller, window controller or perhaps the master table view controller) - but that seems counterintuitive. That seems to defeat the purpose of being able to lay out this stuff within storyboards in the first place, and my controller of choice would then have to know about the other controller specifically.
I must be missing something!
I am using storyboard seques to change views to other parts of my app but when I linked my main view to the next and called for the seque in the function within my enter button it just loops back to my main view and refreshes the app.
Here is my function call:
[self performSegueWithIdentifier:#"Enter" sender:self];
In the storyboard I have my main view linked to the next view with the identifier "Enter" but I still get send back to the same view. Is there any way to specify the destination of the next view? Do I need a certain variable from the other view controllers the need to be imported to the code of the main view controller?
I can provide more code or information if needed.
Picture: http://i1180.photobucket.com/albums/x411/Ryute88/screen.jpg
Identity Inspector Picture: http://i1180.photobucket.com/albums/x411/Ryute88/screen2.jpg
Picture 3: http://i1180.photobucket.com/albums/x411/Ryute88/screen3.jpg
Thanks in advance.
Please check this site: All about storyboarding
You will find details on doing your stuff.Try that and check if the problem still exists!
Are you using the write segue to call on the next view e.g. pop can only be used in case of Navigation Controllers.
In any case the problem will be trivial because storyboards do everything for you.
I've developed an app for Mac OS X Lion using its new view-based NSTableView, but as I want to port the whole app to Snow Leopard I'm trying to figure out the best way to emulate such a tableview. So far I've created a NSCollectionView and everything is fine, except for the fact that I can't get the index of the view from which a button click event is triggered.
In Lion I have the following function:
- (IBAction)buttonClick:(id)sender
so I can get the index of the view inside the tableview using a method (I can't remember its name) like
- (NSInteger)rowForView:(NSView *)aView
with aView being the sender's superview, but I couldn't find something similar for the collection view ... The only "useful" method seems to be
- (NSCollectionViewItem *)itemAtIndex:(NSUInteger)index
(or something like this), but this can't help me as it returns a NSCollectionViewItem and I can't even access it knowing only the corresponding view!
Within buttonClick, try this code:
id collectionViewItem = [sender superview];
NSInteger index = [[collectionView subviews] indexOfObject:collectionViewItem];
return index;
Hope this helps :)
Geesh! Both of those approaches have issues. I can see how the first on may work, but note that the "collectionViewItem" is actually the view, NOT the collectionViewItem, which is a view controller.
The second way will not work, unless you subclass the button and put in a back link to the collectionViewItem. Otherwise, your view does not know what collectionViewItem controls it. You should use a selector binding to the collectionViewItem's representedObject instead, to get the action to the correct object in your array.
How about something like:
id obj = [collectonViewItem representedObject];
NSInteger index = [[collectionView contents] indexOfObject:obj];
As I suggested here: How to handle a button click from NSCollectionView
I would do it like this (because the button you want to press should be coupled with the corresponding model, therefore the represented object):
Add a method to the model of your collectionViewItem (e.g. buttonClicked)
Bind the Button Target to Collection View Item
While binding set model key path to: representedObject
While binding set selectorname to: methodname you chose earlier (e.g. buttonClicked)
Add protocol to your model, if you must tell delegate or establish observer-pattern
use NSArrayController for binding to NSCollectionView,
use collectonViewItem.representedObject to get a Custom Model defined by yourself.
save and get index in your custom model.
That's works for me.
(Abstract: bindings work in code, but not in IB)
I have a window managed by a NSWindowController. To the left of the window is a source view. To the right is a table view showing the elements of the currently selected source.
I have set up a NSTreeController within my window XIB. I want its contents to be used for the source view. It's selection will drive the table view.
I am trying to split this up using NSViewControllers. One view controller will load a NIB containing the source view. Another view controller will load the table view.
Seeing that I need access to the NSTreeController within the source view controller, I have set it to be the view controller's representedObject. (Actually for this setup to be done by the time awakeFromNib is called on the view controller, I have turned representedObject into an IBOutlet).
All works fine when I wire my source view up in code:
[outlineView bind:#"content"
toObject:sources
withKeyPath:#"arrangedObjects"
options:nil];
[outlineView bind:#"selectionIndexPaths"
toObject:sources
withKeyPath:#"selectionIndexPaths"
options:nil];
[[outlineView tableColumnWithIdentifier:#"Title"] bind:#"value"
toObject:sources
withKeyPath:#"arrangedObjects.title"
options:nil];
I am however unable to reproduce this using Interface Builder. Thing is, here the "controller key" textfield is grayed out. Thus I bind column's "value" to the file owner using a model keyPath of "representedObject.arrangedObjects.title". This does not show the desired behavior. Actually an exception is thrown: -[NSProxy doesNotRecognizeSelector:_mutatingNodes] called!
How can I use representedObject in IB?
Can I create a controller in IB which acts as proxy to representedObject?
Could I set-up a tree controller in the source view XIB which during NIB loading gets swapped out for the representedObject?
I moved away from using representedObject. It appears that is meant only for model objects.
I now pass in my tree controller using a custom outlet. I continued setting up and tearing down the bindings in code.
I’ve similar issues when I try to pass a reference to an object controller (NSTreeController in my case). I don’t think this is how Apple wants you to use their KVO-compatible controllers. The exceptions look like they’re XIB-unarchiving & timing-related.
The trick is not to pass the controllers, but to pass the underlying data and keep the selection in sync.
This way you can set up your bindings in a storyboard and won’t get any exceptions. You’ll have to set up a new instance of an object controller for every child view controller (copy & paste in Storyboard once you configured the first one works). For a detailed example take a look at another answer that gets much more into detail.