XCode: link to instance function in comment - xcode

I'm trying to add some documentation to my project, and I was wondering if it's possible to add a "link" to an instance function in another class within a comment. Right now I can simply have something like // see AppDelegate for the response to this notification and I can command-click on AppDelegate to take me to that file, but I'd like to be able to command-click and be taken to a specific function where exactly something like a notification is responded to.
Is this possible?

Related

Change UILabel from appDelegate

I want to do some stuff from the appDelegate in Xcode. One of these things are to change a UILabel.
ViewController *viewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"id"];
viewController.label.text = #"heeej";
I was told this would do the trick. Doesn't work. Label doesn't change. Does anyone know what the problem is?
There are several problems:
Don't do anything in the AppDelegate except for loading the window an the initial view controllers (if needed).
You are instantiating a new view controller in the first line. I assume you already have a view controller and you want to change the label in that view controller. Than you need a reference to that view controller and use this reference to send messages to it.
The label should not be a property of the view controller. You should try to follow the design pattern Model-View-Controller or the pattern Model-View-ViewModel. Ask you preferred search engine what those are if you don't know.
id as an identifier for anything in Objective-C is a bad idea because this is used as an object type.
Edit: You don't need a reference to change a property in the view controller. Use notifications to update the label. The system sends a notification with the name UIApplicationWillEnterForgroundNotification' (see [here][1]). Add the view controller as an observer to the[NSNotificationCenter defaultCenter]` for this name and react on the notification. Read the Apple documentation if you don't know what I am talking about.

NSNotificationCenter in document-based app

I'm using NSNotificationCenter to send custom notifications in a document-based app.
A document-based app can have many open documents. Ideally, I would like the the document and its children to receive only the notifications created within the document or its children. In other words, a document should receive only the notifications that the same document generates.
At first I thought I could use the notificationSender parameter of addObserver:selector:name:object: but then I realised that I don't always know which object will send the notification.
Do I have to check if I'm in the right document for every custom notification? Is there a better way to do this?
I think that your approach works if you use the main document as notificationSender argument for both addObserver:selector:name:object: and postNotificationName:object:.
You can define a NotificationCenter in your NSDocument class and use that to post notifications within a document (Swift):
class Document: NSDocument {
let notificationCenter = NotificationCenter()
// Other stuff
}
And call it like this:
document.notificationCenter.post(name: yourNotificationIdentifier, object: nil)

Creating Bookmarks

Today I'm working with an interesting problem. I'm trying to code simple bookmarks, and by that I mean the most basic functionality; store the url, and load the url when you tap it.
Right now, the idea is that I would use the following:
currentURL = currentWebView.request.URL.absoluteString;
To retrieve the current URL, and then possibly store that within NSDefaults.
(Open to any other suggestions on better ways of preforming this)
However, the problem that I'm having is that, assuming I've gotten that far (retrieved the URL and stored it), how would I go about putting then somewhere in a list format that displays the url that was bookmarked, and then loading them (from that separate bookmarks view) in the main view that the UIWebView is contained in?
Your time is much appreciated
--Jake
NOTE: If you need more information or anything I could possibly help you with in order to come to a solution, just ask
Simple
Suppose your viewcontroller where your webview resides, is WebViewController and where list shows is BookmarkViewController.
Create a property in BookmarkViewController
#property(nonatomic,retain)WebViewController *maincontroller;
Send the reference of your WebViewController to BookmarkViewController when you create BookmarkViewController object
bookmarkobject.maincontroller=self;
and then In didSelectRowAtIndexPath
either use
1) [maincontroller.webView loadRequest:url];
or
make a method in WebViewController with parameter url and call this method from didSelectRowAtIndexPath
2) [maincontroller loadUrlInWebView:url];
and dismiss your modal
or create a property of url in WebViewController and in didSelectRowAtIndexPath
3) [maincontroller urlstring:url];
and in viewWillAppear load this url in your webview
Hope any of these 3 methods will help you.

reference between app controller and window controllers

at the risk of a public flogging, I was hoping someone could clarify my understanding of communicating between the app controller and a window controller. Here is the scenario using xcode4.
using the default AppDelegate.h and .m as the "controller" (which is also a delegate to MainMenu.xib). Have an ivar called int counter.
Created page.xib and PageController.h and .m (subclass NSWindowController). Imported it into AppDelegate.m
Used IBAction to create and view the page object. Like this:
if (!page1) {
PageController *page1 = [[Page
if (!page1) {
page1 = [[PageControoer alloc] initWithWindowNibName:#"page"];
}
[page1 showWindow:sender];
So the new window pops and we can press buttons, etc. The code for the new window is all in PageController.h and .m. and things basically work.
That is the context, here is where I'm confused.
a) question: let's say I want to access that original ivar in AppDelegate.h called counter from PageController. Either retrieving or updating the variable. What approach would I take?
b) confirm: let's say I'm back in the AppDelegate and want to get access to a selector from page1. I believe I can do this as so: [page1 runaction]; or [[page1 variable] setStringValue:#"hello"];
(this complies but I'm not sure it really works because I can't get the changes into the xib view.)
ok and the stumper. Say another view was created with another view controller call it Page2Controller.h and .m.
c) how should data flow between page and page2 -> via the AppDelegate or directly? what would the syntax look like to connect them together?
I've been following tutorials, but they don't really cover this back and forth messaging. Thanks for all the help!
a) Generally, if you want to have data that is accessed by your controllers, it should be in a model which they are given access to in some way. You can access things in the app delegate using this method:
AppDelegate* appDelegate = [[NSApplication sharedApplication] delegate];
[appDelegate <some method>];
b) I don't understand what you're asking. If the app delegate has a pointer to page1, then yes, you can call it directly.
c) Again, you should have a data model of some sort. The controllers should update the data model when the user changes the view. You can use notifications, IBActions, and direct calls to do the updating, for example. You should look up the Model, View, Controller design pattern.

Cocoa NSOutlineView and Drag-and-Drop

I recently started another thread without an account, so I'm reposting the question here with an account so I can edit current links to the program so other users can follow this. I have also updated the code below. Here is my original question:
I read the other post here on Outlineviews and DND, but I can't get my program to work. At the bottom of this post is a link to a zip of my project. Its very basic with only an outlineview and button. I want it to receive text files being dropped on it, but something is wrong with my code or connections. I tried following Apple's example code of their NSOutline Drag and Drop, but I'm missing something. 1 difference is my program is a document based program and their example isn't. I set the File's Owner to receive delegate actions, since that's where my code to handle drag and drop is, as well as a button action. Its probably a simple mistake, so could someone please look at it and tell me what I'm doing wrong? Here is a link to the file: http://dl.dropbox.com/u/7195844/OutlineDragDrop1.zip
You're not responding to NSOutlineView's drag-validation message.
Your original code implemented tableView:validateDrop:proposedRow:proposedChildIndex:. As I pointed out on that question, that's wrong when your table view is an outline view; NSOutlineView will not send a table-view drag-validation message, only an outline-view drag validation message.
You've since changed your drag-validation method to be declared like so:
- (NSDragOperation)outlineView:(NSOutlineView*)view
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(int)row
proposedChildIndex:(NSInteger)index
But nothing actually sends such a message.
Remember that NSOutlineView rarely deals with row indexes, since those can change as parent rows are expanded and collapsed. It deals instead with “items”, which are generally model objects.
Therefore, the correct validation method is:
- (NSDragOperation)outlineView:(NSOutlineView*)view
validateDrop:(id <NSDraggingInfo>)info
proposedItem:(id)item
proposedChildIndex:(NSInteger)index
Notice the name of the third component of the selector, and the type and name of the argument that goes with it.
With this change applied, your data source validates drops.

Resources