Assign string to UITextView - iphone

I am trying to load text into a textview. This seems simple enough:
box.text = #"hello";
NSLog(#"Box.text contains: %#", box.text);
The problem is that this NSLog just keeps printing null.
(I have IBOutlet UITextView *box declared.) I imagine that since box.text is null, that's why nothing is actually showing up in the UITextView either.
Anyone know what's going wrong?
Thanks a lot!

If you're calling box.text from your controller's init method, your IBOutlet hasn't been loaded from the nib file yet – so box is still pointing to nil, and the text assignment is going nowhere. You'll need to wait until viewDidLoad or later to successfully access the properties of your IBOutlet.

Related

tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0xebba1b0

I understand the reason why message sent to deallocated instance 0xebba1b0 is called, it is because I am sending a message to an object which is no longer in memory.
So here's my scenario. I have a ZoomedViewController which has a UITableView in it. The UITableView has a custom UITableViewCell, which has an attributed label as a subview. When a link is pressed on the attributed label (which in turns triggers didSelectRowAtIndexPath) it delegates to my MainViewController and calls the method closeZoomedImageVC in MainViewController:
-(void) closeZoomedImageVC
{
[self.zoomedImageContainer_ removeFromParentViewController];
[self.zoomedImageContainer_.view removeFromSuperview];
}
the issue is that when that didSelectRowAtIndexPath is triggered, then zoomedImageContainer_ is already gone. How do I solve this then?
To illustrate the point better, basically when I do:
[self performSelector:#selector(closeZoomedImageVC) withObject:nil afterDelay:1.0];
this doesn't cause the crash anymore, but this is not a solution as it is hacky. What this does is it lets didSelectRowAtIndexPath to be executed first before it is deallocated.
Store a reference to your UITableView in ZoomedViewController:
#property (nonatomic, strong) IBOutlet UITableView *tableView;
Make sure to connect the outlet in Interface Builder. Now, when your zoomedImageContainer_.view is removed, it won't dealloc the UITableView until you release that reference as well.
You also need to store a strong reference to your ZoomedViewController in MainViewController, and only set that to nil after you have saved the selected row back in MainViewController.
When tapping on a cell in a table will invoke an animation to highlight selected cell, and it needs a short duration complete, so the best way is what you are doing, perform selector after a delay, I think 0.5 second is enough.
I found the solution to my self is to just set the allowSelection = NO in the tableView property. This will let the attributedLabel inside the UITableViewCell to have interaction but will disable didSelectRowAtIndexPath being called

My objects in storyboard don't link correctly with the code

I am trying to use storyboards in my application. I began well, I have added two view controllers with a button to go from the first to the second.
The problem is in this second view controller: the objects I add on it and link with the code do not work. When I set a breakpoint in the view controller code, the 'self.property' is set to nil, whereas it should be instantiated by the storyboard.
I have been looking for an answer for hours, and I really don't understand the problem, since all the rest seems to be good.
I have tried to write the property in the code (strong/nonatomic, nonatomic/retain and even weak/nonatomic), to link the object directly to the code so that it creates the property automatically, but I never get anything else than "nil" with breakpoints.
viewController.h:
#interface NMLoadingViewController : UIViewController
{
__weak IBOutlet UIProgressView *imageProcessingProgressView;
}
#property (weak, nonatomic) IBOutlet UIProgressView *imageProcessingProgressView;
#end
.m:
#synthesize imageProcessingProgressView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Custom initialization
NSLog(#"INIT.");
}
(amont other lines of irrelevant code)
If I set the breakpoint to the "INIT." line, my imageProcessingProgressView is nil. I can't see a reason for that, I guess I have missed a very little detail...
(Note that I have tried this with other objects, like a UILabel, but it did not work either. Stranger, when I had two objects, one of them had an adress which was not nil, but still it was corrupted and did not show the right object.)
Edit: It was a lot of trouble for nothing... The problem is about the Variable View in XCode, which show my variable to "nil", whereas a log shows me a correct object...
Remove this...
{
__weak IBOutlet UIProgressView *imageProcessingProgressView;
}
...and make the property strong.
Change your log message to...
NSLog(#"INIT: %#", self.imageProcessingProgressView);
...and see if you still have a problem. If you do, then take another look at your storyboard connections.
Make sure NMLoadingViewController is the class on your viewController
First try out the answer by #Eric because I do believe that is the real issue.
Also, make sure that you are actually using the interface builder to hook the controls back to the view controller. This is done by dragging a line from the control back to the property of the view controller.

Weird behavior of UILabel

I have a UILabel Outlet, and everytime I access its text property with multiple characters, then it will crash and gives me EXC_BAD_ACCESS. Its very weird and I can't find any solution for this issue.
Thanks.
sasayins
When it got EXC _BAD _ACCESS, would you mind get the backtrace and post it here?
Or better: post the code where you access/modify the UILabel.
Did you synthesize the property? are you releasing it somewhere by mistake? gotta put some code up in order for us to help you...
You probably forgot to link the outlet in Interface Builder to the desired UILabel. This means that the UILabel is currently nil (non existent). Drag a line from the file owner to the UILabel to connect the outlet in Interface Builder.
Make sure that your outlet does not have the same name as a property of one of the super classes of your view controller. For example, UIViewController has a title property, and if you create an outlet with the name of "title" you are overriding this property and will most likely have problems.

uiview getter nil

I am trying to get certain iVars from my view controller to my view (a subview) so drawrect will draw a shape based on user inputs. When I hard code those variables into the subview, the shape draws perfect, but when I use getters to access the custom variables from the view controller, they come back nil and the drawing is messed up. I get no errors or warnings. I know there is a problem with the getter, any suggestions? Anybody have an example of passing variables to a subview so cgcontext can be used in the drawrect? I am stuck big time. I assume at this point my problem is so simple, I am missing something fundamental. I thought I had setters/getters down packed.
Make sure that whatever you're getting 'from' (ie, the parent, or view, or whatever you're accessing the getters on) is not nil. Try setting a break point right before accessing them and validating that your targets are valid, and have valid data.
We'd really need to see more code. Ben was saying that, somewhere in your view, you must set parent = (some code to get a reference to my parent). Make sure this actually works, i.e. you really do have a reference to your parent. Even if the ivars are correct, if your parent is nil, then sending [nil getSomeiVar] will return 0 in most cases.
Without a better understanding of the code, this is my guess. Somewhere in your UIViewController, you should have something like:
myView = //make your view
[myView setParent:self]
Parent is a variable of the view, with a property so that you can access it
Edit:
Why does it matter if we know what you are making?
For setParent to work, you need to following:
an ivar YourUIViewControllerSubclass *parent; in your view's .h
a property for this ivar #property (nonatomic, retain) YourUIViewControllerSubclass *parent; in your view's .h
A synthesis for this ivar #synthesize parent; in your view's .m inside the #implementation
Yeah, you were both wrong, but I took a lot away from you helping me though. I didn't have to post code after all. I had to pick up my debugging skills big time. Someone in another post mentioned violations of the MVC. I had to set the ivars in the uiview, to the custom values FROM the view CONTROLLER. In other words, I had to set from the controller rather than getting from the subview.

navigationItem.title doesnt get refreshed

I have a RootViewController and a SubViewController. I traverse the views in either direction. The RootViewController is a UITableView SubClass. Depending upon the row selected I want to change the title of the subview using self.navigationItem.title = [chunks objectAtIndex:1];
It works when the subview is first time loaded. But when I return to the RootViewController and load the subview again the previous title persists.
Any ideas what am I missing out on?
In your particular case, you probably want to set your title in viewWillAppear: so that the title gets set every time the view comes on the screen.
I just tried it in an app of mine. When I set the title using
self.navigationItem.title = #"Foo"
the name in the navigation bar changes instantly. I think you have a bug somewhere else that you code is only getting called the first time you invoke your SubViewController. Stick a break point on that line and see if it actually gets called a second time. Or perhaps [chunks objectAtIndex:1] is always returning the same string.
Or perhaps I'm not understanding your question. As far as I can tell it does work like you are expecting it to.
I guess you are trying to change the title in the viewDidLoad method. The viewDidLoad is called only the first time the view is loaded. In case you are reusing the same viewcontroller's instance, viewDidLoad will be called only once.
Instead try setting the title in viewWillAppear method. This method is called every time the view is going to be displayed. That should work.