Help with TextViews

What I am trying to do is allow a user to place notes (in the form of UITextViews) on the screen, and be able to place any number of them. I am not an experienced programmer for the iPhone, so I need a little input here. It will actually be an iPad app, and what I want to know are these things:

How do I set up, allocate, and use a UITextField in C++?

How can I create an arbitrary number of them?

How do I manipulate their contents and positions?

Any help is greatly appreciated, and if anyone has good examples on using Objective-C objects like the UITextView object in C++, that would also be of much help.

As far as I know it’s not possible to interface with UIKit objects using c++. They are all objective-c classes and should be talked to in objective-c. It it possible to mix objective-c and c++ though, so if you want to keep things clean it’s a good idea to write a c++ wrapper for the objective-c object you want to use. So you write your own c++ class, but change the extension to .mm to allow mixing c++ and objective-c, and in that class you write methods which control the objective-c class. Because you’re writing it yourself you can choose which functionality you need and how you want to interface with that from within your application. You’d write the wrapper as if you were writing a ‘pure’ obj-c iphone app, so you’d have to read up on that I guess.

You can also choose to change testApp.cpp to testApp.mm and forget about the whole wrapping thing, but it might get messy. If you do so, make a clear distinction between your obj-c and c++ or you will get lost somewhere in between… :slight_smile:

Thanks for the clarification. It’s not that I mind using C++ mixed with Obj-C, I was just wondering about how exactly one would initialize and use Obj-C objects in C++. Is it done just like in C++, or is there a differing syntax? That’s what I was wondering.

But I have looked around the internet some more and found further instructions. It seems that, aside from using [] brackets for functions, it is very similar. What confuses me, though, is that there is still dot notation for some functions and accessors, and I’m having trouble distinguishing.

Thanks for the clarification. It’s not that I mind using C++ mixed with Obj-C, I was just wondering about how exactly one would initialize and use Obj-C objects in C++. Is it done just like in C++, or is there a differing syntax? That’s what I was wondering.

It’s exactly the opposite, it’s done just like in obj-c. So you use the obj-c syntax, which needs some getting used to when you’ve got a c++ background. Apple documentation is OK, I’d say start with this-one and you’ll get there.

What confuses me, though, is that there is still dot notation for some functions and accessors, and I’m having trouble distinguishing.

That is called ‘properties’. In the header file (‘interface’) you can use the ‘@property int myNumber’ statement, and if you say ‘@synthesize myNumber’ in your implementation, objective-c just did you a favor and created a getter and a setter for you. These are accesible by using the bracket syntax:

  
  
int myOtherNumber = [myClass myNumber];  
[myClass setMyNumber:yetAnotherNumber];  
  

By tweaking the ‘@property …’ statement you change the getters and setters that are created, so you can set wether to assign, retain or copy, whether the variable is atomic or not, and if you are allow to readonly or readwrite:

  
  
@property(nonatomic, retain) int myNumber;  
@property(readonly, copy) int otherNumber;  
  
etc...  
  

As I said, these are accesible using the bracket syntax. If you use the dot syntax, you don’t use the getters and setters obj-c created for you. You are directly editing the member variables of the class, which isn’t necessarily a bad thing, but most of the time Apple says it is:

  
  
int myOtherNumber myClass.myNumber;  
myClass.myNumber = yetAnotherNumber;  
  

Remember, all the fancy memory management stuff objective-c put in the getters and setters are being skipped right now, so you’ll have to take care of that yourself.

I hope this clarifies the dot-syntax a bit.

Oh and I almost forgot:

In your header:

  
  
#import <UIKit/UIKit.h>  
  
...  
  
UITextView *myTextView;  
  

In your implementation:

  
  
myTextView = [[UITextView alloc] initWithFrame:CGRectMake(someX, someY, someWidth, someHeight)];  
[ofxiPhoneGetGLView() addSubview:myTextView];  
  

I never did this directly in a GL view so I can’t really predict how this will work out for you, but give it a try and let us know :wink:

Thanks again! I’ve read into the Apple documentation, and it’s making sense so far. I threw together a little test application, and adding textViews to the GL view seems to work okay. It properly intercepts touch events when tapped, and passes all others to the main application. You have clarified things quite a bit.