Deallocating an object's properties in Objective-C - ios

I currently have a header that displays a name, time, and a couple of buttons. This header should only appear if an appointment is selected in a dashboard, which is irrelevant here. However, once i logout and log back in, with no patient selected, the header view is displayed. I think this is because I did not deallocate the appointment object, and i'm not sure how to do that (i'm new to iOS programming).
Here's my code:
So I have the interface
#interface DashboardVC : CommonVC <UIActionSheetDelegate, HeaderViewDelegate, PracticeServiceDelegate> {
IBOutlet HeaderView *_headerView;
}
And inside the HeaderView object i have these properties:
#property (strong, nonatomic) CCAppointment *appointment;
#property (strong, nonatomic) IBOutlet UIButton *backButton;
#property (strong, nonatomic) IBOutlet UIView *currentPatientView;
#property (strong, nonatomic) IBOutlet UIImageView *avatarImageView;
#property (strong, nonatomic) IBOutlet UILabel *patientNameLabel;
I then, in dashboard VC, want to deallocate, but i'm not sure how... this is what i have:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
How do I deallocate the properties so that the headerVIew does not show up?

if you want to destroy _headerView try:
[_headerView removeFromSuperview];
_headerView = nil;

Related

Custom UIView with UILabel iOS

I have a UIView with two UILabel that I want to reuse in more than one UIViewController. I use storyboard and I assign a custom class alertView:UIView that I declared.
file AlertRemote.h
#interface AlertRemote: UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
-(void) setTextLabel;
file AlertRemote.m
-(void) setTextLabel{
[ _label1 setText:#"attention..."];
[_label2 setText:#"the program..."];
}
file Controllo.m
//the view present in the storyboard alertView linked to the uiview
#property (strong, nonatomic)IBOutlet AlertRemote *alertView;
#property AlertRemote *alRemView;
[super viewDidLoad];
_alertView=_alRemView; [_alertView setTextLabel];
[_alertView setTextLabel];
if I put some breakpoints inside setTextLabel the code don't works
thanks!!
In order for the custom alert to work, you need to initialise it.
AlertRemote *alRemView;
alRemView = [[AlertRemote alloc]init];
[alRemView setTextLabel];
I think you are not initializing the property alertview. If you are initializing the property alertview then try to set directly with out using alremView.

How to change value for UITextField and UISwitch from childViewController in parentViewController

I found a lot of how to use methods of chlidViewController. But I couldn't find how to change and set value of uitextfield and uiswitch from childViewController.
ChildViewController.h:
#protocol VVInformationTableViewControllerDelegate;
#interface VVInformationTableViewController : UITableViewController
#property (weak, nonatomic) id<VVInformationTableViewControllerDelegate> delegate;
#property (weak, nonatomic) IBOutlet UITextField *nameTextField;
#property (weak, nonatomic) IBOutlet UITextField *surnameTextField;
#property (weak, nonatomic) IBOutlet UITextField *emailTextField;
#property (weak, nonatomic) IBOutlet UITextField *locationTextField;
#property (weak, nonatomic) IBOutlet UITextField *headlineTextField;
#property (weak, nonatomic) IBOutlet UITextField *positionTextField;
#property (weak, nonatomic) IBOutlet UITextField *companyTextField;
#property (weak, nonatomic) IBOutlet UISwitch *messagesEnable;
#end
ParentViewControler.m:
- (void)viewDidLoad
{
self.currentAttendee = [VVAPIClient sharedClient].currentUser;
NSParameterAssert(self.currentAttendee);
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
self.infoTableController = [[VVInformationTableViewController alloc] initWithNibName:#"InformationTableViewController" bundle:nil];
[self addChildViewController:self.infoTableController];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.infoTableController.nameTextField.text = self.currentAttendee.firstName?:#"";
self.infoTableController.surnameTextField.text = self.currentAttendee.lastName?:#"";
self.infoTableController.emailTextField.text = self.currentAttendee.email?:#"";
self.infoTableController.locationTextField.text = self.currentAttendee.location?:#"";
self.infoTableController.headlineTextField.text = self.currentAttendee.headline?:#"";
self.infoTableController.positionTextField.text = self.currentAttendee.position?:#"";
self.infoTableController.companyTextField.text = self.currentAttendee.company?:#"";
}
-(void)viewDidLayoutSubviews{
self.infoTableController.messagesEnable.on = NO;
self.infoTableController.nameTextField.tag = 0;
self.infoTableController.surnameTextField.tag = 1;
self.infoTableController.emailTextField.tag = 2;
self.infoTableController.locationTextField.tag = 3;
self.infoTableController.headlineTextField.tag = 5;
self.infoTableController.positionTextField.tag = 6;
self.infoTableController.companyTextField.tag = 7;
}
Thanks for help.
As david says in his comment, don't.
It violates the encapsulation of the other view controller, and leads to spaghetti code.
You should treat another VCs (View Controller's) views as private.
What you should do is add properties to the child view controller to hold strings and other state data that you need to display. Then in your child view controller's viewWillAppear method, you can take the settings and apply them to your view hierarchy.
In your case, since what you're doing is displaying a whole bunch of information about "currentAttendee", (which I guess is a model object) you might want to think about passing a pointer to the whole attendee object to the child, and letting it display the information itself.
Or, of the child can edit the object, you might want to pass a copy, and use a delegate method when you want to commit the changes made in the child, or simply return if you want to discard changes.

Memory leak iPhone and UIPageViewController

I have a problem with big memory leaks when I use UIPageViewController. I have a view and I scroll it. I see that memory grows very fast.
I think that is the problem:
- (ViewControllerEvent *)viewControllerAtIndex:(NSUInteger)index {
ViewControllerEvent *childViewController = [[UIStoryboard storyboardWithName:[self isIPad]?#"Main_iPad":#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"ViewControllerEvent2"];
childViewController.index = index;
childViewController.event = [self.events objectAtIndex:index];
return childViewController;
}
My class:
#interface ViewControllerEvent : UIViewController
#property (assign, nonatomic) NSDictionary *event;
#property (assign, nonatomic) NSInteger index;
#property (weak, nonatomic) IBOutlet UIImageView *header;
#property (weak, nonatomic) IBOutlet UIScrollView *_scrollViewDesc;
#property (retain, nonatomic) IBOutlet UITextView *descLabel;
#property (retain, nonatomic) IBOutlet UIView *whiteBackground;
#property (weak, nonatomic) IBOutlet UIImageView *bootomMapImage;
#end
I use about 100 views in my pager.
Memory leaks are usually related to keeping strong references to some object which should have a weak reference, delegates for instance. Either that or you're keeping references to each child view controller, thereby growing the size of memory.
Don't use an instance method to vend view controllers for the page view controller; use a class method instead.
+ (ViewControllerEvent *)viewControllerAtIndex:(NSUInteger)index
{
…
return childViewController;
}

iOS objects return nil soon after creation

so I'm writing Obj-C for iOS, and something "strange" is happening..
I have an MVC, with a UITableView (private):
#interface MVC ()
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) CellVC *cell1;
#property (strong, nonatomic) CellVC *cell2;
#end
I load the table view up with a few custom cells. My custom cell class is actually a UIViewController... so I instantiate a few, and set cell.contentView to the corresponding CellVC.view inside the tableView:cellForRowAtIndexPath: method. My custom cell class:
#interface CellVC : UIViewController
#property (strong, nonatomic) IBOutlet UIKnob *knob1;
#property (strong, nonatomic) IBOutlet UIKnob *knob2;
#property (strong, nonatomic) IBOutlet UIKnob *knob3;
#property (strong, nonatomic) IBOutlet UIKnob *knob4;
#end
In case you're wondering, I've written a subclass of UIControl named UIKnob...
In CellVC's viewDidAppear: method, I've set a breakpoint to check the values of every knob. Each knob is non-nil, so I am happy... they have all been created.
My goal is to set MVC as the delegate of each knob. (4 knobs for each cell)
If I set a breakpoint anywhere in MVC, the value of each knob is nil??...
cell1.knob1.delegate = self;
will not work because the knobs only exist inside CellVC.m ...
Any ideas??

Changing outlet object properties

I have a view controller alertForNeedsClassification as a property in another class, as such:
#interface SCAAppDelegate()
{
HomeScreenViewController * _homeScreenViewController;
NSInteger SCAStatus;
}
#property (strong, nonatomic) PromptClassifyViewController * alertForNeedsClassification;
#end
#implementation SCAAppDelegate
#synthesize alertForNeedsClassification;
#synthesize window = _window;
PromptClassifyViewController's interface looks like this:
#interface PromptClassifyViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *headerTitle;
#property (weak, nonatomic) IBOutlet UITextView *message;
#property (weak, nonatomic) IBOutlet UIButton *notNowButton;
#property (weak, nonatomic) IBOutlet UIButton *classifyButton;
#property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
#property (weak, nonatomic) IBOutlet UIView *alertView;
#property NSUInteger tag;
#property (weak, nonatomic) IBOutlet id<PromptClassifyViewControllerDelegate> delegate;
- (void)show;
- (void)showFromView:(UIView *)view;
- (IBAction)show:(id)sender;
- (IBAction)dismiss:(id)sender;
- (IBAction)buttonWasPressed:(id)sender;
- (void)setHeaderTitleWithText:(NSString *)text;
#end
I am trying to change the values of IBOutlets message and headerTitle text, like this:
alertForNeedsClassification = [[PromptClassifyViewController alloc] initWithNibName:#"PromptClassifyViewController" bundle:nil];
//[alertForNeedsClassification setDelegate:self];
self.alertForNeedsClassification.headerTitle.text = #"A title";
alertForNeedsClassification.message.text = #"A message";
Then I show alertForNeedsClassification calling a show method (it's like a custom uialertview, but it doesn't subclass from uialertview).
Thing is, no matter how I change it, the text on alertForNeedsClassification.view is always that which is defined in the nib, ie. I can't change it programmatically.
My custom alert view is based on Jeff LaMarche's design: http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
Any ideas what might be going on?
Please be careful when you allocate and initialize the UIView object, especially if you trying to mix using Nib and dynamically generating objects. The best place is within -(void)awakeFromNib or -(void)viewDidLoad
Also, make sure these methods are called. By using -(id)initWithNibName:bundle: only cannot make sure your view to be loaded. Try -(void)addChildViewController and -(void)addSubview: on parentViewController's view to make sure view is loaded after being initialized.
If the text had to be prepared before being loaded, assign it to separate NSString property within PromptClassifyViewController class. Since this property is independent from view being loaded, you can change it's value BEFORE view is appeared. Make sure this text is used and applied to the headerTitle within -(void)show method.
Since you allocate PromptClassifyViewController and access weak referenced headerTitle from self. alertForNeedsClassification, make sure it's not deallocated right afterward.
Usually, weak option is not used for IBOutlet properties. Though it is used when generating outlet connection code by dragging objects from Interface Builder. Try testing your code using strong.
I was assigning values to the IBOutlets before they were alloc'd/initialized. The solution I implemented was to set the values I needed to non-IBOutlet properties (NSStrings in this case) and assign those where needed, in Prompt...Controller's viewDidLoad;

Resources