I am just starting out at Xcode, and I'm trying to make a OS X web browser.
I want to figure out when my webview is loading and when it isn't. I've already looked at a lot of pages both from here and the Apple Developer Library on this, and this is what I get:
- (void)webViewDidStartLoad:(WebView *)webView {
//enter code here
}
And of course I also saw the webViewDidFinishLoad void, but when I try this in my AppDelegate.m nothing happens. I have connected the webview's frameLoadDelegate to the App Delegate and from what I understand, I also need to use the <> protocols in the AppDelegate.h file. My problem is that when I type in webFrameLoadDelegateProtocol into the <>s it tell me that webFrameLoadDelegateProtocol doesn't exist.
It appears to be because you haven't set the webView delegate. You do not need to add the protocol to your header though. Instead you need to add this code somewhere, I suggest in applicationDidFinishLaunching
[webView setFrameLoadDelegate:self];
Then you can override the methods. If that doesn't work, then make sure you have connected your webView from the header to the webView in your IB. Also be sure to synthesize the webView in the .m.
Finally, you could use my open source example for an OS X browser. It is under the MIT license, so you can use it freely.
https://github.com/JosiahOne/basic_cocoa_web_browser
EDIT
I just realized, you are using the wrong method for Cocoa. Use these methods instead.
-(void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
//Did start Load
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
//Did finish Load
}
My problem is that when I type in "webFrameLoadDelegateProtocol" into the "<>"s it tell me that "webFrameLoadDelegateProtocol" doesn't exist.
WebFrameLoadDelegate Protocol is an informal protocol. It is not eligible for adoption in the same way. Omit <webFrameLoadDelegateProtocol> from your class's #interface.
when I try this in my AppDelegate.m nothing happens. I have connected the webview's frameLoadDelegate to the App Delegate
When do you set the web view's frameLoadDelegate property to be your app delegate?
Related
I am new to osx developing I have read on documentations about windowDidResized:
method , but I am failing to get its delegate .
It is never get called for me , I have included appKit/appKit.h as it said in docs
but the delegate method never triggers
(I am trying to get it inside my NSViewController)
can some one please make a simple example how do i get that delegate please?
what I have tried to do is:
-(void)loadView
{
//blabla
self.view.window.delegate = [self.view.window delegate];
//blabla ..
}
- (void)windowDidResize:(NSNotification *)notification
{
NSLog(#"window Resized");
}
I am expecting non xib usage samples please :)
thanks a lot in advance.
A view probably shouldn't be a window's delegate.
Normally the delegate for a window would be a controller object in the Model View Controller paradigm.
You can however use NSNotificationCenter to add an object as an observer for a specific NSNotification from a specific object.
( be sure to remove the observer in its dealloc method if not earlier )
NSWindow class sends many different notifications.
I simply updated to iOS 7.1 and I get an unrecognized selection error for a function called "_layoutCells".
I have a simple subclass of UITabBarController.
Note that this is a hack to avoid a bad crash until a better solution or explanation is found. I though I should share it.
Simply add the following method to your UITabBarController subclass implementation:
- (void) _layoutCells
{
// HACK ALERT: on iOS 7.1, this method will be called from deep within the bowels of iOS. The problem is that
// the method is not implemented and it results in an unrecognized selected crash. So we implement it...
//
// What could go wrong?
}
Thanks to GenesisST for giving his answer, but I know methods are called for a reason. And usually layoutCells will call layout for all subviews. While I rather wait for an answer from Apple, I like other people need to submit my app within a given timeline.
In my case, I was getting this error due to some hacks. I had replaced the UIMoreNavigationController delegate, which is an undocumented class by Apple, so I could expect errors. I am doing this hack to extend the More tab's functionality, and this error only occurs when I change the moreNavigationController tableView's delegate.
So I store their delegate, change it, then call _layoutCells to their delegate when iOS calls it on my class.
- (void)_layoutCells
{
if([self.moreTableViewDelegate respondsToSelector:#selector(_layoutCells)]){
[self.moreTableViewDelegate performSelector:#selector(_layoutCells)];
}
}
I don't know if this apply's to anyone here, but just in case someone else comes to SO with my edge case.
I've had the same issue in my app where I have provided custom delegate/datasource to the more tableview controller. I haven't figured out why, but it seems that _layoutCells method is invoked on the more tableview controller.
I fixed it, adding this method:
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
// self.viewController is my tabBarController
UINavigationController* moreNavigationController = self.viewController.moreNavigationController;
// Retrieve the more list controller (it is the first in the hierarchy)
id moreListController = moreNavigationController.viewControllers.firstObject;
Class moreTableViewClass = [moreListController class];
if (moreTableViewClass) {
return [moreTableViewClass instanceMethodSignatureForSelector:aSelector];
}
return nil;
}
I've done various test and it seems a reliable workaround. But if you'll find better solution... share it!
I need a method like - (void)applicationDidFinishLaunching:(NSNotification *)aNotification but I need it for my WebView IBOutlet WebView*webView;. What is the right method?
You have a few different options with WebView, as there are around 5 informal delegates which you can implement methods to:
For example, you might want to implement the frame load and resource load delegates to monitor the load progress and display status messages. Applications that use multiple windows may want to implement a user interface delegate. See the individual informal delegate protocols for more details: WebFrameLoadDelegate Protocol Reference, WebPolicyDelegate Protocol Reference, WebResourceLoadDelegate Protocol Reference, and WebUIDelegate Protocol Reference.
I find WebFrameLoadDelegate to be the easiest, just set yourself as the delegate:
[webView setFrameLoadDelegate:self];
Then implement this method:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
source: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/Reference/Reference.html
- (void)webViewDidFinishLoad:(UIWebView *)webView
It's in UIWebViewDelegate protocol.
First Bound ResourceDelegate of webview with file Owner
(void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
This will work for me perfectly.
I'm new to iPhone development. I've been reading several questions on how to make a google maps annotation callout window accept line breaks. Every tutorial I've read requires me to fire the mapView:didSelectAnnotationView method. But I have no idea how to trigger this. things I've tried include
putting the method in my MapViewController.m file which extends UIViewController
putting the method in a MapView.m file which extends MKMapView, then have my Mapview element in my storyboard reference it as the class to use
There's so much about xcode, objective c, and iphone development that I don't understand, so i can't tell where my problem lies.
At the moment, my map does plot my desired marker on the desired location. I just need to understand how to fire the mapView:didSelectAnnotationView and mapView:viewForAnnotation functions before I can start customizing the call out box.
Does anyone have step by step instructions on how to trigger these functions?
A bit of background
A few things to note:
You don't call mapView:didSelectAnnotationView. The MKMapView calls that function on it's delegate. In other words, when you set up an MKMapView, you tell it: "hey, listen, anytimme you need to tell me what's happening on the map, go tell this guy, he'll handle them for you". That "guy" is the delegate object, and it needs to implement mapView:didSelectAnnotationView (that's also why its name "did select", ie, it already happened, as opposed to "select"). For a simple case, the delegate is often the UIViewController that owns the MKMapView, which is what I'll describe below.
That method will then get triggered when the user taps on one of your annotations. So that's a great spot to start customizing what should happen when they tap on an annotation view (updating a selection, for instance).
It's not, however, what you want if you want to customize what annotation to show, which is what it sounds like you're actually after. For that, there's a different method just a few paragraphs earlier on the same man page: mapView:viewForAnnotation. So substitute this method if you find that mapView:didSelectAnnotationView isn't what you were looking for.
What you can do
If you got as far as a map with a marker, I'm guessing you have at least:
* a view controller (extendeding from UIViewController, and
* an MKMapView that you've added to the view for that view controller, say named mapView
The method you want to fire is defined as part of the MKMapViewDelegate protocol.
The easiest way to get this wired is to:
make your UIViewController the delegate for you MKMapView
in code, say in your viewDidLoad, of your MapViewController.m you could do mapview.delegate = self, OR
in Interface Builder, you could drag the connection from the the MKMapView delegate property to the file's owner
then, define a method on your UIViewController called mapView:didSelectAnnotationView, declaring it just like the protocol does, in your MapViewController.m file:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
// whatever you need to do to your annotation and/or map
}
Good luck!
mapView:didSelectAnnotationView is a delegate method of the map view, you can read about it here:
MKMapViewDelegate Protocol Reference
You don't need to call it, the map view will call it "by it self" and send it to every view/view controller that registered as it's delegate.
What do you need to do
Basically you need to add the MKMapViewDelegate on your .h file, what will look something like this:
#interface someViewController : UIViewController <MKMapViewDelegate>
Then in the .m file, after you instantiate the map view you should add:
mapView.delegate = self;//were self refers to your controller
From this point and on your controller will be able to "receive messages" from the map view which are the methods that you can see on the MKMapViewDelegate reference I linked to.
So to implement the mapView:didSelectAnnotationView you need to add in your .m file
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
//if you did all the steps this methosd will be called when a user taps the annotation on the map.
}
What is happening
What happens in the background is:
The map view has a method (Apple codded) that handles the AnnotationView touch events.
When a touch event take place it sends a "message" to all of it's delegates saying "Hey a user did Select Annotation View on this map, Do with it what ever you need".
usually it looks like that:
[self.delegate mapView:someMapView didSelectAnnotationView:someAnnotationView];
Then every view/controller that assigned itself as a delegate and implemented the method will cal this method.
Good luck
Place *place = [[Place alloc] init];
PlaceMark *placeMark = [[PlaceMark alloc] initWithPlace:place];
[self.mapView selectAnnotation:placeMark animated:YES];
How do I handle a mousedown event inside a window in Cocoa?
My code:
-(void)mouseDown:(NSEvent *)event {
NSLog(#"yay");
}
I am using Mac OS10.6, in xcode 4.0.1.
EDIT:
Yes, this is in the app delegate, but this is my .h:
#interface jumperAppDelegate : NSWindow {
Which I have done before in app delegates (just not for mouse events). This is really annoying me
Make sure you inherit from NSWindow, as well as conform to the <NSWindowDelegate> protocol. Like this:
#interface YourWindow : NSWindow <NSWindowDelegate> {}
#end
Then you should receive the event notification.
-(void)mouseDown:(NSEvent *)event {
}
For this method to be called the class it is being called in needs to inherit from NSResponder. Windows and views are all subclasses of NSResponder. If the class you are calling this from is not a subclass of NSResponder then the method will not fire.
* Update *
Also be sure to override acceptsFirstResponder to return yes.
- (BOOL)acceptsFirstResponder {
return YES;
}
I don't know for sure, but I have heard that in your header file (.h) that you need to replace the "NSObject" with "NSWindow". I would test it but I am not at my computer right now.
Also, make sure that you put the following code into your header file:
- (void) mouseDown:(NSEvent*)event;
EDIT: I have done some tests and research, but I cannot get it to work. I have two tips though.
Use the '-acceptsFirstMouse method.
Try creating an NSEvent:
NSEvent * someEvent;
-(void)mouseDown:(NSEvent*)someEvent;
This probably won't work, but I will have more information tomarrow