Update uitextView from another controller - objective-c

I want to update uitextview from another uiviewcontroller classes.
Can anyone help me?

It is not exactly clear what you are asking, but I think you mean that you want to send a message to a UITextView instance within another UIViewController subclass.
So long as this UITextView is a member of this UIViewController, you can send messages to it. (Of course, it must be declared as a property for getters and setters).
Example:
OtherViewController *otherViewController = [[OtherViewController alloc] init];
[[otherViewController textView] setText:#"your text here"];
[[self navigationController] pushViewController:otherViewController animated:YES];
I suppose this could be useful if you were about to switch push or pop a view controller and needed to add information to it before you did so. Hopefully this answered your question.

you can update your textview's text using NSNotificationCenter from other viewcontroller.

Related

When is it safe to manipulate screen controls through code in iOS/Objective-C

I have a question regarding iOS (or perhaps more accurately Objective-C) and properties. I have a UIView with a UISegmentedControl, by default it has 3 segments. I have a message which accepts a parameter and based on this parameter I may want to remove one of the segments. In UIView A I do this:
MyViewController *myview = [[[MyViewController alloc] initWithNibName:#"MyViewController" nib:nil] autorelease];
[[self navigationController] pushViewController:myview animate:YES];
[myview showItem:item];
In UIView B this happens in showItem:
-(void) showItem:(Item*)item{
if (item.removeSegment){
[segmentControl removeSegmentAtIndex:0 animate:NO];
}
}
I have noticed that the segment only gets removed when I call showItem after I have pushed it on the navigation controller. When I swap those two line, so I first call showItem and then push the view, the UISegmentedControl still has three segments instead of two.
This just feels wrong, it seems like bad practice that my code will break if someone doesn't call two messages in the right order. Is there a better way to do this? I've been looking at some sort of a property lifecyle that I can use, I am very familiar with this from ActionScript 3, but I have been unable to find anything on the subject.
(as an aside: in AS3 I would make a property, in the setter I don't manipulate any screen controls but call InvalideProperties. My overriden methode CommitProperties will be called once the entire object and child controls have been created. In CommitProperties I check if my property value has changed and this is where I would remove the segment.)
A common way of doing something like this is to create an Item *item property in MyViewController and set that when myview is created. So, your code becomes:
MyViewController *myview = [[[MyViewController alloc] initWithNibName:#"MyViewController" nib:nil] autorelease];
myview.item = item;
[[self navigationController] pushViewController:myview animate:YES];
MyViewController would then use that property in its viewWillAppear: method to configure its own segment control.
I think what you are falling prey to is myview->segmentControl doesn't exist until myview.view is referenced because of the lazy load of the view.
-(void) showItem:(Item*)item{
[self view]; // NO OP TO FORCE LOAD!!
if (item.removeSegment){
[segmentControl removeSegmentAtIndex:0 animate:NO];
}
}
Should work for you. Hope this helps!

AppDelegate can't add subView

Since appDelegate does not have a view, just window, its hard to figure out how to load a view from it. My problem has for long been that when didReceiveLocalNotification fires i cant load a new view with that event. I have been working around it til the point that i must do something about it. When i tries to addSubview, xcode gives me the error:
Receiver tupe 'UIWindow' for instance messages does not declare a method with selector 'addSubView'
for this: (at [self.window addSubView:view];)
screwLightBulbViewController *view = [screwLightBulbViewController newMyView];
[self.window addSubView:view];
I understand that the appDelegate file does'nt have a addSubview but i want to switch to a particular view when it fires.
I have tried many other ways, like calling a function in screwLightBulbViewController and make a view from that. My function in the viewController now looks like this:
+(id)newMyView
{
UINib *nib = [UINib nibWithNibName:#"MyView" bundle:nil];
NSArray *nibArray = [nib instantiateWithOwner:self options:nil];
screwLightBulbViewController *me = [nibArray objectAtIndex: 0];
return me;
}
any help in any way would be realy appreciated and thanks for you time. :)
It's addSubview not addSubView:. UIWindow is a subclass of UIView.
Adding a view directly as a subview to the window is not usually recommended, so instead you should try and add the view as a subview to the top controller view. If you can spare some time you should look over the view programming guide and view controller programming guide, it will be useful in the future.

Passing text from UITextView to string in different ViewController

I need to pass the text of a UITextView in viewController1 to viewController2 and set the text to a string in viewController2. I thought I had the code figured out, but it turns out that nothing is being passed.
In viewController2.h I added
viewController1*v1
and assigned a property to it.
in the viewController2.m
-(void)method{
v1=[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil];
NSString *text=[[NSString alloc]init];
[v1.tweetText setText:text];
}
This is probably the wrong way to go about it, so does anyone have other solutions to this problem?
Thats the correct way to send data to another vc, but you are not setting the variable text to anything. Try setting it to hello and see if you get hello as the tweetText in the other vc.

how to use UITableViewStyleGrouped at the initialization?

i found this link : initialize UITableViewController with [super initWithStyle:UITableViewStyleGrouped]
but i don't understand the way to initialize the tableView as "grouped" inside the controller class?
self = [super initWithStyle:UITableViewStyleGrouped];
Could you help me?
Thanks a lot
Paul
[self.tableView initWithFrame:self.view.frame style:UITableViewStyleGrouped];
The easiest way is to change the TableView in Interface Builder to Grouped style in the Attribute Inspector. To do this in code you would need to change the way the UITableViewControler is created. You would not do it through the xib, but in code in the AppDelegate. You could also change the code to a UIViewController and implement the table view delegates and change things yourself.
Not sure if this is what you are looking for, but assuming you have a UITableViewController called myTableVC that is created via code (with no xib) and you want the table to display with a grouped style, you could do so by calling the view controller as follows:
MyTableViewController *myTableVC= [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self presentModalViewController:myTableVC animated:YES];
[myTableVC release];
Similarly, if you wanted a plain tableviewcontroller style, you could do so by:
MyTableViewController *myTableVC= [[MyTableViewController alloc] initWithStyle:UITableViewStylePlain];

Design an NSView subclass in Interface Builder and then instantiate it?

So I have an NSTabView that I'm dynamically resizing and populating with NSView subclasses. I would like to design the pages in IB and then instantiate them and add them to the NSTabView. I got the programmatic adding of NSView subclasses down, but I'm not sure how to design them in IB and then instantiate them.
I think I got it. Let me know if this is not a good thing to do.
I made a new xib file, set its File's Owner to be an NSViewController and set its "view" to the custom view I designed in the xib.
Then you just need:
NSViewController *viewController = [[NSViewController alloc] initWithNibName:#"MyViewXib" bundle:nil];
NSView *myView = [viewController view];
#toastie had a really good answer. Mine is similar, but requires a bit more explanation.
Let's say you've already got a controller object and you don't want to instantiate a new controller object just to get at a view, and let's say that you're going to need multiple copies of this view (for example, you've designed a custom UITableViewCell in IB and you want to instantiate it again and again from your UITableViewController). Here's how you would do that:
Add a new IBOutlet to your existing class called "specialView" (or something like that). It may also be helpful to declare it as a (nonatomic, retain) property.
Create a new view xib called "SpecialView", and build the view however you like.
Set the File's Owner of the view to be your controller object.
Set the specialView outlet of File's Owner to be the new view.
Whenever you need a new copy of the view in your code, you can simply do the following.
(gratuitous text to get formatting working properly)
NSNib * viewNib = [[NSNib alloc] initWithNibNamed:#"SpecialView" bundle:nil];
[viewNib instantiateNibWithOwner:self topLevelObjects:nil];
[viewNib release];
NSView * myInstantiatedSpecialView = [[[self specialView] retain] autorelease];
[self setSpecialView:nil];
Yes, it's a bit more code than other ways, but I prefer this method simply because the view shows up in the designated IBOutlet. I retain and autorelease the view, because I like to reset the outlet to nil once I have the view, so it can be immediately ready to load a new copy of the view. I'll also point out that the code for this is even shorter on the iPhone, which requires one line to load the view, and not 3 (as it does on the Mac). That line is simply:
[[NSBundle mainBundle] loadNibNamed:#"SpecialView" owner:self options:nil];
HTH!