-1

In my project, I have two UIViewControllers in a tab bar application, each one contains a UITableView which displays information from RSS feeds. The two view controllers are supposed to be perfectly identical, except for a single NSString parameter (the feed URL).

I know that I can simply copy and paste the code from one UIViewController to another, but I was wondering if there is a better way to do this. I'm not sure if I'm phrasing this correctly, but I think I want to create a separate file which contains a UIViewController "instance" and apply that instance to each view controller in my app.

I'm wondering if something like I'm asking for is possible, and how it would be done.

Charles
  • 4,372
  • 9
  • 41
  • 80
  • 1
    Have just one, and pass the URL as a parameter!!! – Merlevede Mar 09 '14 at 20:38
  • looking on the other question you just ask: you don't spent any time with documentation, do you? – vikingosegundo Mar 09 '14 at 20:38
  • @Merlevede It's a tab bar application, having just one wouldn't work because the "state" of each UIViewController would not be independent. – Charles Mar 09 '14 at 20:49
  • @vikingosegundo Do you mind linking me to some related documentation that would be useful for me in implementing this? – Charles Mar 09 '14 at 20:50
  • 1
    @goddfree I know you need two! Have just one class, and create two instances. YOur class would have a URL property. And I would follow vikingosegundo's advice. – Merlevede Mar 09 '14 at 20:52
  • sure: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011210 – vikingosegundo Mar 09 '14 at 20:52
  • and [this](https://www.google.de/search?client=safari&rls=en&q=starting+with+objective+c&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=pdQcU8OKIYirtQbby4DAAg) – vikingosegundo Mar 09 '14 at 20:53
  • It sounds like you need a good ObjC book or series of tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Mar 09 '14 at 21:20

3 Answers3

3

I'm assuming these 2 view controllers are both installed as tabs of your tab bar controller.

If that's the case, then you want the two view controllers to be different instances of the same view controller class. Identical twins, if you will. Let's call it MyRSSTableViewController. You'd just give the MyRSSTableViewController class a feedURL property, and set that property as part of creating each instance of your MyRSSTableViewController class.

This is a fundamental concept of object-oriented programming, and if you don't get it then you need to stop and do some reading. You might want to check out the "Objective-C programming: The Big Nerd Ranch Guide". That book will teach you programming in C and Objective C from the beginning.

If you already have programming experience in other procedural languages then that book might not be the best choice for you. Tell us about your current skills and we can make better recommendations.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • This is the answer you want, it will avoid creating 2 different `UIViewControllers` with the same code. My answer was a bit off so it's better you don't go that route so I took it down. – JMarsh Mar 09 '14 at 21:00
  • I've worked quite extensively in web development, primarily PHP and JavaScript. I understand that there are significant differences between object-oriented and procedural languages. For the most part I just look at other people's examples and code snippets to get a general idea of "what's going on", and it's true that a lack of documentation reading on my part is why I fail to understand basic instantization concepts. Truth be told, I prefer to understand code simply by jumping in and exposing myself to it, rather than reading through documentation and then trying to apply it. – Charles Mar 09 '14 at 21:04
  • Thank you for your advice, I will look in on it and see if I can produce the results I want. – Charles Mar 09 '14 at 21:05
  • If you've got experience in procedural languages then the Big Nerd Ranch book would not be a good choice. You want a book that teaches you object-oriented design, objective-C, and the Cocoa Touch frameworks. It takes a shift in your thinking, and you are going to drive yourself nuts until you make that shift. Spend the time early to learn the concepts and you will save yourself a lot of pain. – Duncan C Mar 09 '14 at 21:17
  • About what you said earlier, do you mean that I should create a new instance of `MyRSSTableViewController` within each tab bar view controller, and then display it using something like `presentViewController`? – Charles Mar 09 '14 at 21:30
  • Well, I meant that the view controllers that you install in the tabs of your tab bar controller should be instances of MyRSSTableViewController. – Duncan C Mar 09 '14 at 21:48
0

Personally I would just have the title switch and have the data source of the array change based on a bool value. and then call reload when you want to switch.

But the best method is definitely Duncan C's

Charlie
  • 222
  • 3
  • 20
-2

what you can do is overload the constructor of your UIViewController (just like you would do with any class) and add the url as parameter.
Using this approach, you would create 2 instances of the UIViewController with the only difference in the parameter used when creating them.

Esteban 8A
  • 33
  • 4