user172902
user172902

Reputation: 3601

Create TableViewController and UIViewController that both inherits from a BaseViewController

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 { }

Upvotes: 3

Views: 1643

Answers (3)

hasan
hasan

Reputation: 24185

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.

Upvotes: 0

Ravi
Ravi

Reputation: 2451

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()

Upvotes: 1

Dravidian
Dravidian

Reputation: 9945

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.

Upvotes: 5

Related Questions