ios - How to call one class delegate methods from another class -


in table list using swift here want load 1 class table-list in class view controller , concept working in objective-c come down swift delegate methods not calling objective -c@swift codes below please me 1 else

backgroundview.h:-

#import <uikit/uikit.h> @interface backgroundview   uiviewcontroller<uitableviewdatasource,uitableviewdelegate> {      uitableview *tableview; } @property(nonatomic, retain) uitableview *tableview; -(void)tablelist:(uiview *) view1; @end 

backgroundview.m:-

#import "backgroundview.h" @interface backgroundview () {     nsarray * mainarray; } @end @implementation backgroundview @synthesize tableview; - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib. }  -(void)tablelist:(uiview *) view1 {     mainarray = [[nsarray alloc]initwithobjects:@"india",@"australia",@"usa", nil];      tableview=[[uitableview alloc]init];     tableview.frame = cgrectmake(0,0,320,400);     tableview.datasource=self;     tableview.delegate=self;     tableview.autoresizingmask = uiviewautoresizingflexibleheight | uiviewautoresizingflexiblewidth;     [tableview registerclass:[uitableviewcell class] forcellreuseidentifier:@"cell"];     [tableview reloaddata];     tableview.separatorcolor = [uicolor blackcolor];     self.tableview.tablefooterview = [[uiview alloc] initwithframe:cgrectzero];     [view1 addsubview:tableview]; }  - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return mainarray.count; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview {     return 1; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cell";     uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:cellidentifier   forindexpath:indexpath] ;      if (cell == nil)     {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];     }     cell.textlabel.text= [mainarray objectatindex:indexpath.row];      return cell; }  -(void)tableview:(uitableview *)tableview willdisplaycell:(uitableviewcell *)cell forrowatindexpath:(nsindexpath *)indexpath {     if ([cell respondstoselector:@selector(setseparatorinset:)]) {         [cell setseparatorinset:uiedgeinsetszero];     }     if ([cell respondstoselector:@selector(setpreservessuperviewlayoutmargins:)]) {         [cell setpreservessuperviewlayoutmargins:no];     }     if ([cell respondstoselector:@selector(setlayoutmargins:)]) {         [cell setlayoutmargins:uiedgeinsetszero];     } } @end 

maindview.h

#import <uikit/uikit.h>  @interface viewcontroller1 : uiviewcontroller  @end 

maindview.m

#import "viewcontroller1.h" #import "backgroundview.h"  @interface maindview () {     backgroundview * v1; }  @end  @implementation maindview  - (void)viewdidload {     [super viewdidload];     v1 = [[backgroundview alloc]init];     // additional setup after loading view.     [v1 tablelist:self.view]; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. } @end 

now it's working fine in objective-c , table list loading fine

come down swift ios:-

now calling tableviewadding mainview class backgroundview it's calling delegate methods not calling in background class i.e showing empty view controller in swift table list not loading please me , swift code

backgroundview.swift

import uikit class backgroundview: uiviewcontroller,uitableviewdelegate, uitableviewdatasource  {      var tableview: uitableview  =   uitableview()     var items = nsarray ()      override func viewdidload() {         super.viewdidload() }     func tableviewadding(myview:uiview)     {         items = ["india","australia","usa"];         println(items)         tableview.frame         =   cgrectmake(0, 50, 320, 200);         tableview.delegate      =   self         tableview.datasource    =   self         tableview.registerclass(uitableviewcell.self, forcellreuseidentifier: "cell")         myview.addsubview(tableview)     }     func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         println("numberofrowsinsection")         return self.items.count     }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {           println("cellforrowatindexpath")          var cell:uitableviewcell = tableview.dequeuereusablecellwithidentifier("cell") uitableviewcell          cell.textlabel?.text = self.items.objectatindex(indexpath.row) nsstring          return cell     }      func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath){         println("in first")       }     override func didreceivememorywarning() {         super.didreceivememorywarning()     } } 

mainview.swift

import uikit  class mainview: uiviewcontroller{      @iboutlet var myview1: uiview!      override func viewdidload() {         super.viewdidload()          var total = backgroundview.alloc()         total.tableviewadding(self.view)     }     override func didreceivememorywarning() {         super.didreceivememorywarning()     }  } 

[updated] you're not initializing backgroundview in mainview.swift. you're allocating memory object. note both alloc , init class in objective-c. in swift, have same thing class.alloc().initialize(). however, swift has replaced verbose line of code simple call: class()

objective-c:

class *myinstance = [[class alloc] init]; 

swift:

var myinstance = class() 

some other things:

  • it's idea call tableview.reloaddata() after change information in (like in tableviewadding) method.
  • it's never idea hard code numbers (like table view size).
  • tableviewadding looks class method. tableviewadding follow camelcase convention more accurately

documentation why using var myinstance = class.alloc() not same using var myinstance = class() can found in apple's nsobject documentation under creating, copying, , deallocating objects

https://developer.apple.com/library/prerelease/ios/documentation/cocoa/reference/foundation/classes/nsobject_class/index.html#//apple_ref/occ/clm/nsobject/alloc


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -