Is it possible for me to navigate to a view that has uitextfield and make it so that uitextfield doesn't change everytime I navigate?
If I understood correctly, you want a text that was previously, there...
there is a lot of options but an easy an fast one is have a static NSString....
so in your class you have the uitextfield, in that class you define
static NSString *myString;
then, when the user or you modify it and you have access to it, define it in the static NSString.
Now, to recover it, in the method you initialize that view you do:
myTextField.text = myString;
that, will restore the text.
Related
I want to create a NSWindow of same NSWindowController class type each time user sends an action.
This is my Code.
objController = [[MyController alloc] initWithWindowNibName:#"MyController"];
[objController showWindow:nil];
This is a simple two liner which gives me NSWindow. but the thing is, If I don't make a class level object, the window doesn't get displayed. So, I had to make a class level object. It worked well and gave me NSWindow of type MyController.
But, since it is a class level object, If I want to trigger this action every time user clicks on a button, previous window gets closed. And new window gets appeared.
I don't want this to happen. I want to keep all the previous NSWindows in memory and user can interact with them.
How do I do it ?
I think, this should be something small but at the time I don;t have any solution in my hand.
Kindly help me to get this.
Thank you.
My guess is that by "class level object" you mean a #property of type MyController, which can indeed only hold a single window controller. If you need to store several instances of MyController and do not want to create a separate #property for each of them, you need to put them into an array type, namely a NSMutableArray.
Add a #property NSMutableArray *myControllers to your class and initialize the array (for example in the - init method or your class) with self.myControllers = [NSMutableArray array].
Now you can add newly created window controllers to it with [self.myControllers addObject:] which makes them stay in memory instead of overwriting each other by sharing the same property for storage.
Is there a way, using the Interface Builder, in XCode to display a random image? So let's say I have three images and want a random one to display each time my app is loaded. I'm new to XCode, so I'm looking for the simplest way to do it and IB seems to be the route to take. Thanks!
After thinking about this for a few seconds, I realized the answer is of course you can, but it is such a pain, that I doubt that you would want to. I came up with a approach as an example of how to use IB in such a way.
First, subclass UIImageView, I will call it MyRandomImageView. The only member of MyRandomImageView is #property (retain, nonatomic) NSString *randomImageJSON.
Next, add a UIImageView to your view. In the Identity Inspector, change the Custom Class from UIImageView to MyRandomImageView. Then in the Identity Inspector, under User Defined Runtime Attributes, set randomImageJSON to something like [ "image1.png", "image2.png", "image3.png" ].
The final part is where you have all the trouble. Create -[MyRandomImageView setRandomImageJSON]. It needs to set the _randomImageJSON iVar, use NSJSONSerialization to create the array of strings, randomly pick one of the strings, then set self.image = [UIImage imageNamed:randomImage]
This should do exactly and you wish, a random image from the Interface Builder. Hope that helps.
You have to do this programmatically. Haven't tested the code below, but I'm pretty sure it works.
int imageNumber = arc4random() % 3;
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%i", imageNumber]]];
You should first declare it in the implementation and link it to your UIImageView in the Interface Builder.
I created Constants.h and Constants.m as suggested here:
Constants in Objective-C
Now I want to also use some strings defined in Constants.h in Interface Builder to for example set the text of a label.
How to do this using bindings? I think I should somehow use an ObjectController (Mode: Class, Class Name: Constants)? But what would be the Content Object for this controller (since I don't have any variable of type Constants)? Maybe use a Singleton in Constants.m? Any suggestions?
I don't think you can bind the strings in any way.
I would recommend do it in code in viewDidLoad.
Note that string constants aren't so great for texts in UI.
EDIT:
Xibs have their own localization system but I don't think it's very good. It basically means creating a new xib for every language. If you support only one language, just put your strings into the xib and the problem is solved.
NOTE: the following is an idea for my current project and I haven't implemented it yet but I suppose it would let us easily add new language translations.
My idea for a better xib localization is to define IBOutlet for every localizable component (e.g. myButton1, myTextField1) and then write a file with localized strings (xml, properties, plist whatever) where every string is keyed by the IBOutlet name, e.g:
myXib1.myButton1.selected.title = This is a button.
myXib1.myTextField1.placeholder = "This is text field placeholder"
Then, you have to write a method which takes the xib name, finds current language and goes through all string properties for the given xib. It can use [NSObject performSelector:] to access the IBOutlet getters:
id localizableView = [self performSelector:NSSelectorFromString(#"myButton1")];
and you call this method from viewDidLoad (or you create a UILocalizedController class which calls it automatically and all your controllers will be its descendant).
Also note there is NSLocalizedString class which should help you with localization.
I have an ImageKit program that needs to be able to save images, so I'm using the IKSaveOptions accessory view, to allow the user to choose a file type, etc.
However, I want to remove some of the options, and add a checkbox to the panel for TIFFs. Alternatively, I want to add a type of file. However I can't figure out how to do this. I assume I'm going to have to subclass off of IKSaveOptions and over-ride something, but I can't find any sample code or documentation that tells me how to do this.
ETA: In particular, I need to be able to allow the user to distinguish between multi-page tiff, and a bunch of single-page tiffs with a page-number appended.
ETA: So, SO is telling me "I have only a few hours before the bounty expires so I should choose an answer" But ... there are no answers! (You'd think that SO would be smart enough to see that, but oh well B-)
IKSaveOptions is inherited from NSObject. Real accessory view is built internally and is not accessible with public API:
#interface IKSaveOptions : NSObject
{
#private
void * _privateData;
id _saveOptionsView;
}
So your best bet is to build your own save dialog accessory view with blackjack and hookers. You can start with NSView custom view in interface builder. Then it is simply:
NSSavePanel *saveDialog = [NSSavePanel savePanel];
[saveDialog setAccessoryView:mySaveAccessoryView];
VB is right, but you can get the accessory view from the NSSavePanel after it has been set by the IKSaveOptions object.
I have view0 through view25. I don't particularly want to have a 25-case switch, so is there a way to do something like this?
- (void)modifyViewNumber:(int)number
{
[view*number* dosomething];
}
Put the views in an array at startup.
You could put a tag on each view and use a for loop with the following method:
- (id)viewWithTag:(NSInteger)aTag
Nobody's suggested NSMatrix yet? This sounds like what it's made for.
The caveat is that the views must all be controls, such as text fields or buttons. Basically, look at its class reference and see whether it inherits from NSControl at some point. If it passes that test, then NSMatrix is an option. (Here's the list of all AppKit classes, to make this easy.)
To make a control into a matrix in IB, create one of the control in the usual way, then option-drag its resize handle. It won't appear to do anything at first, but keep dragging. Instead of resizing, your control will proliferate; it is now a matrix of cells instead of a single control.
Why don't you put the views into an array and obtain references to them using the array index?
- (void)modifyViewNumber:(int)number
{
UIView* view = [views objectAtIndex:number];
[view dosomething];
}
Many people have said use an array--I do this all the time, it's really the only way to go when using an interface builder, but I wanted to add a little.
Sometimes getting the values into the array can be tricky. There is usually a way to get an array of all controls on the screen. usually I'll grab that and iterate over it looking for a type of control with a certain naming pattern and collect those into my own array.
In this way, you can often add a new control with no code change whatsoever (always one of my favorite goals).
Create an array of views. Add each one, in order, then reference it by the array index.
If the numbers are not sequential, use a hash table.
Reflection might be an option, but could be slower (I'm not an Objective C guru, don't even know if that is practical).
I assume you're using Interface Builder to setup those views?
Someone may have to correct me on this, but I believe you can create an NSArray Interface Builder outlet in your class and then assign all your views to it. For example, you could declare "IBOutlet NSArray * views;" in your header file, and then use interface builder bindings to tie all 25 views to that property. They'll automatically be added to the array, and then you can cleanly iterate over it in your code.
Either use a collection or Reflection.
Assuming Interface Builder (otherwise straightforward as answered before). This is the best I could manage:
In .h:
{
UILabel *banner[NUM_BANNERS];
}
#property (nonatomic, retain) IBOutlet UILabel *banner0;
#property (nonatomic, retain) IBOutlet UILabel *banner1;
#property (nonatomic, retain) IBOutlet UILabel *banner2;
etc.
In .m:
- (void)viewDidLoad
{
banner[0] = banner0;
banner[1] = banner1;
banner[2] = banner2;
etc.
Then can access by array index. Remember to release.