Tuesday, 4 June 2019

I implemented the iOS delegate Method it was working in initial stage

Native script version 5.1.1

Xcode 10 and above, Mojave OS

SearchBar Onloaded Function

//Delegate Method Call
let delegate = MallSearchDelegate.initWithOriginalDelegate((<any>this.searchbar)._delegate);
(<any>this.searchbar)._delegate = delegate;

Delegate Class

export class MallSearchDelegate extends NSObject{
public static ObjCProtocols = [UISearchBarDelegate];
private _originalDelegate:UISearchBarDelegate;

public static initWithOriginalDelegate(originalDelegate: UISearchBarDelegate):MallSearchDelegate{
    let delegate = <MallSearchDelegate>MallSearchDelegate.new();
    delegate._originalDelegate = originalDelegate;
    console.log("Initialized SearchBar");
    return delegate;
}

public searchBarTextDidEndEditing(searchBar: UISearchBar):void{ 
    console.log("endSearch");
    if(((<any>this._originalDelegate)._owner)!=undefined){
        const owner = (<WeakRef<SearchBar>>(<any>this._originalDelegate)._owner).get();
        if (owner) {
            owner.notify({
                object: owner,
                eventName: "endSearch",
                focusable:false
            });
        }
    } 
}

public searchBarTextDidBeginEditing(searchBar: UISearchBar):void{ 
    console.log("beginSearch");
    if(((<any>this._originalDelegate)._owner)!=undefined){
        const owner = (<WeakRef<SearchBar>>(<any>this._originalDelegate)._owner).get();
        if (owner) {
            owner.notify({
                object: owner,
                eventName: "beginSearch",
                focusable:true
            });
        }
    }
}

public searchBarSearchButtonClicked(searchBar: UISearchBar) {
    console.log("onSubmit");
    searchBar.resignFirstResponder();
    if(((<any>this._originalDelegate)._owner)!=undefined){
    const owner = (<WeakRef<SearchBar>>(<any>this._originalDelegate)._owner).get();
        if (owner) {
            owner.notify({ 
                object: owner,
                eventName: "submit"
            });
        }
    }
}

Navigate to another page and back to the page when the user clicks the search bar the app was crash.

Reason come back to the delegate method page and click the search bar returns the error owner is undefined that's why I added the conditional checking for undefined.



from I implemented the iOS delegate Method it was working in initial stage

No comments:

Post a Comment