3

I have five view controllers that all inherits from one base view controller. My baseVC contains shared functions such as starting or stopping activity idnicator or checking for internet activity. The VCs looks like below

class BaseVC: UIViewController { }
class NewsFeedVC: BaseViewController { }
class MakePostVC: BaseViewController { }
class NotificationVC: BaseViewController { }
class MoreVC: BaseViewController { }
class CollectionVC: BaseViewController { }

My NewsFeedVC and NotificaitionVC (from the storyboard) are constructed by normal UIViewController with tableView dragged in. So this all works. However, I am thinking about changing these two VC from the storyboard to be UITableViewController instead of tableView dragged into viewController. The reason for that is becuase there were some bugs around pull to refresh causing tableView to jump if the tableView was constructed inside UIViewcontroller.

However, if NewsFeedVC and NotificationVC inherits from UItableViewController like below, I will not be able to use the functions inside BaseVC anymore. How can I structure this to achieve what I desire?

class NewsFeedVC: UITableViewController { }
class MakePostVC: UITableViewController { }
user172902
  • 3,541
  • 9
  • 32
  • 75

3 Answers3

5

Setup protocols in your BaseViewController with functions that you want your NewsFeedVC class to inherit and then call them using delegate

In you BaseVC add this :-

  prtotocol baseVCDelegate{

     func activityIndic()
     func checkInternetConnect()
 }

  class BaseVC : UIViewController{
     ....
    var delegate : baseVCDelegate!
   ....
    ..
   func activityIndic(){
   ..
    ..
    }
   ..

   func checkInternetConnect(){
   ..
    ..
    }
   }

in your NewsFeedVC declare a variable of type BaseVC

 var baseVCHandler : BaseVC = BaseVC()

assign its delegate to self in viewDidLoad()

 baseVCHandler.delegate = self

Then access any function that your protocol conforms !

PS:- go through this https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem, might help you understand the reason behind why swift doesn't like blunt multiple inheritance of classes, Will clear your basics.

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • how you assign "baseVCHandler.delegate = self" even you are not inherited newsfeed class from baseVCDelegate Protocol? – Jon Striker Jul 22 '21 at 08:46
1

create an extension for UIViewController and place all those common methods in the extension

extension UIViewController{

    func showActivityIndicator()
    {
         // write your code to show Activity Indicator
    }
    func hideActivityIndicator()
    {
        // write your code to hide Activity Indicator
    }

    func checkInternetConnection() -> Bool
    {
        return true // write your code to check connection
    }
}

to create an extension xcode File menu -> New -> File -> and select Swift File and name it as your wish. And create extension for any class like above.

Later you can call these methods like self.hideActivityIndicator()

Ravi
  • 2,441
  • 1
  • 11
  • 30
0

I suggest to fix the pull to refresh bug instead of missing up with your structure. I use pull to refresh in a UIViewController with a UITableView in it with no problems.

refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[refreshControl setTintColor:[self.stylingDetails themeColor]];
[oppTableView addSubview:refreshControl];

- (void)refresh:(UIRefreshControl *)refresh {
}

Note:

a UITableViewController loses features that a UIViewController have. That may cause you a new problems that can't be solved depend on the functionality of your controller.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • That would be ideal! However, did you experience this "jump" problem for pull to refresh? Please see http://stackoverflow.com/questions/31011734/why-uirefreshcontrol-jumping Pretty much when you "slowly" pull down the tableview to refresh, near the end the tableView jumps down 20 to 30 points – user172902 Aug 24 '16 at 15:28