Reputation: 5
I'm a begginer and in my last question I learn a little about the MVC pattern. But I still have some doubts. I am working as a Junior, modifying a program. Behind I will leave the principal question.
The last question was:
"I'm trying to send a segmented control variable to another file but I cant get it, I think I'm quiet close, but I need a little help, what's wrong?"
--<AdSettingsTVC.h>--
@interface AdSettingsTVC : UITableViewController
@property (strong, nonatomic) NSString *serverName;
@property (strong, nonatomic) IBOutlet UISegmentedControl *servidorControl;
-(IBAction)getServer:(UISegmentedControl *)sender;
@end
--<AdSettingsTVC.m>--
@interface AdSettingsTVC () <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
//General Settings
@end
@implementation AdSettingsTVC
- (IBAction)getServer:(UISegmentedControl *)sender{
ANLogDebug(@"%@ setServidor | Server changed to Settings: %ld", CLASS_NAME, (long)sender.selectedSegmentIndex);
if (sender.selectedSegmentIndex == 0){
self.serverName = @"Server 1";
} else if (sender.selectedSegmentIndex == 1){
self.serverName = @"Server 2";
} else if (sender.selectedSegmentIndex == 2){
self.serverName = @"Server 3";
}
}
--<AnAdFetcher.m>--
- (void)requestAdWithURL:(NSURL *)URL {
AdSettingsTVC *settings = [[AdSettingsTVC alloc] init];
NSString *nameServer = settings.serverName --> it's null
self.URL = URL ? URL : [self adURLWithBaseURLString:[NSString stringWithFormat:@"http://%@", nameServer]];
}
The answer to this question was that I was not using the MVC pattern, so when I call AdSettingsTVC.alloc.init the values are initialized to nil. Then I read that I have to use a model to declare logical operators.
Now, I realize that I have two model files, and , but I dont know what to declare in both files in order to get the variable "serverName" in the file I have shown you above, .
What do I have to declare in model files and how do I get the variable from AnAdFetcher.m
Thank you very much for your time
Upvotes: 0
Views: 62
Reputation: 6211
When you first initialize your viewcontroller by doing
AdSettingsTVC *settings = [[AdSettingsTVC alloc] init];
All the variables inside it are set to nil
or 0 (they actually could be garbage values as well, but assume they are unusable in their current form). So if right after that you ask for a variable:
NSString *nameServer = settings.serverName;
it will return nil. I'm guessing that you think it should return based on:
if (sender.selectedSegmentIndex == 0){
self.serverName = @"Server 1";
} else if (sender.selectedSegmentIndex == 1){
self.serverName = @"Server 2";
} else if (sender.selectedSegmentIndex == 2){
self.serverName = @"Server 3";
}
but you have not called that function yet, you have only initialized the controller. If you would like the serverName
to be valid immediately after the controller initialization like this then you are going to have to put a custom initializer (or overload the basic one) in your controller class. In that initializer you are going to have to create your UISegmentedControl
and then set the serverName
based on it's initial settings. I'm not sure when all the visual objects are created and usable when using storyboard or xib initialization, so your best bet would be to create a function inside your view controller (that is available directly after initialization) something like:
-(NSString*)retrieveServerName:
{
[self getServer:mySegmentedControl];
return self.serverName ? self.serverName : @"initial case when serverName is nil";
}
And just as a point of opinion, the method called getServer
is really a setting method, not a getting method. You might think about renaming that to something more in line with what the function does.
Upvotes: 1