Search code examples
objective-cmacoscocoaibaction

How call in -(IBAction)


I'm new in objective-c. Need to make a button link, which will open in a new window. To do this I need to do IBAction to create a new window. This add WebView :

- (ERBrowser *)addWebView:(NSURL *)url{
    for (NSView *view in browserViews)
        [view setHidden:true];
    ERTabModel *newModel = [[ERTabModel alloc] init];
    NSTabViewItem *newItem = [[NSTabViewItem alloc] initWithIdentifier:newModel];
    [tabView addTabViewItem:newItem];
    [tabView selectTabViewItem:newItem];
    ERBrowser *browserView = [[ERBrowser alloc] initWithFrame:mainView.frame];
    [browserView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
    [mainView addSubview:browserView];
    [browserViews addObject:browserView];
    [browserView setUIDelegate:self];
    [browserView setFrameLoadDelegate:self];
    if (url)
        [[browserView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
    return browserView;
}

This link for button :

- (IBAction)bookmarkButton:(NSButton*)sender
{
    ERBrowser *browserView = [browserViews objectAtIndex:[tabView indexOfTabViewItem:[tabView selectedTabViewItem]]];
    [[browserView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[sender alternateTitle]]]];
    [self addNewTab:(id)];

And this IBAction add new tab :

- (IBAction)addNewTab:(id)sender {

    [self addWebView:(NSURL *)];
}

I can not understand that as a cause addWebView [self addWebView:(NSURL *)]; And how to add addNewTab [self addNewTab:(id)];

Please explain how to do it?


Solution

  • The syntax (type), e.g. (NSURL *), in a method declaration defines the type of a parameter, e.g.:

    - (ERBrowser *)addWebView:(NSURL *)url
    

    declares a method addWebView:, which takes a single parameter of type NSURL *, and within that method the parameter is referred to using the name url.

    Within an expression the syntax (type) is a cast operation, an operation which changes the static type of its sub-expression. For example:

    (double)3
    

    takes the integer expression 3 and casts it to a double value.

    In your method call:

    [self addWebView:(NSURL *)];
    

    you are calling addWebView: and passing it as argument the result of the expression:

    (NSURL *)
    

    which is half of a cast expression - you've provided no value to cast. Hence the error.

    What you need is a value of type NSURL * to pass to addWebView:, from your code this value will be the URL of the page you wish to load. You have to decide where you obtain this from.

    HTH