I know about addButtonWithTitle function, but I dont want to create new buttons, only to fill the Title area in an UIActionSheet.
If it is possible, is it possible with UIAlertView?
NSMutableString *mstr = [[NSMutableString alloc]init];
for (NSString *s in array)
[mstr appendString:s];
UIActionSheet*actionSheet = [[UIActionSheet alloc] initWithTitle:mstr ......
[actionSheet showInView:view];
[actionSheet release];
[mstr release];
This is assuming yer array contains strings.
Similar approach for UIAlertView.
Related
I am using MASPreferences in my app, I was able to set up everything correctly, with 3 different views (preferences, login and about).What I would like to do is to choose which panel gets shown when the window is opened. This is so that when the user clicks on about, the about panel is shown, etc. instead of the last panel shown being the one displayed. As of now I have tried modifying the entry in the plist file, but it does not seem to work. Is there any other way?
So after a bit of trying, and by using #Jasper's answer, I came up with the following:
-(void)openPreferencesWindowWithIdentifier:(NSString *)identifier {
[NSApp activateIgnoringOtherApps:YES];
[[NSUserDefaults standardUserDefaults] setValue:identifier forKey:#"MASPreferences Selected Identifier View"];
// Create the preferences window
NSViewController *generalViewController = [[GeneralPreferencesViewController alloc]initWithNibName:#"GeneralPreferencesViewController" bundle:nil];
NSViewController *loginViewController = [[PushoverLoginViewController alloc]initWithNibName:#"PushoverLoginViewController" bundle:nil];
NSViewController *aboutViewController = [[AboutPreferencesViewController alloc]initWithNibName:#"AboutPreferencesViewController" bundle:nil];
NSArray *controllers = [[NSArray alloc]initWithObjects:generalViewController,loginViewController,[NSNull null],aboutViewController, nil];
NSString *windowTitle = NSLocalizedString(#"Preferences", #"Comon title for preferences window");
_preferencesWindowController = [[MASPreferencesWindowController alloc]initWithViewControllers:controllers title:windowTitle];
[self.preferencesWindowController showWindow:nil];
}
Essentially this method writes the required "tab" on the plist file, and then initalizes a new instance everytime. By doing so, the correct view is loaded. The identifier parameter is the one you set up for each of the views. Thanks again to Jasper for his answer, really helped me understand how to figure this one out!
MASPreferences remembers the last opened 'tab'
Change in the order in your array when passing it to your MASPreferencesWindowController should work to change the order of your tabs.
-(NSWindowController *)preferencesWindowController
{
if (_preferencesWindowController == nil)
{
NSViewController *generalViewController = [[GeneralPreferencesViewController alloc] init];
NSViewController *accountViewController = [[AccountPreferencesViewController alloc] init];
NSViewController *troubleshootingViewController = [[TroubleShootingPreferencesViewController alloc] init];
//Change the order here
NSArray *controllers = [[NSArray alloc] initWithObjects:accountViewController, generalViewController, troubleshootingViewController, nil];
NSString *title = NSLocalizedString(#"Preferences", #"Common title for Preferences window");
_preferencesWindowController = [[MASPreferencesWindowController alloc] initWithViewControllers:controllers title:title];
}
return _preferencesWindowController;
}
Have a look inside MASPReferencesWindowController.m line 6. There is a static NSString key which handles the logic to show the last selected tab
static NSString *const kMASPreferencesSelectedViewKey = #"MASPreferences Selected Identifier View";
The key is used in:
- (void)windowDidLoad
{
if ([self.title length] > 0)
[[self window] setTitle:self.title];
if ([self.viewControllers count])
self.selectedViewController = [self viewControllerForIdentifier:[[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesSelectedViewKey]] ?: [self firstViewController];
NSString *origin = [[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesFrameTopLeftKey];
if (origin)
[self.window setFrameTopLeftPoint:NSPointFromString(origin)];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidMove:) name:NSWindowDidMoveNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidResize:) name:NSWindowDidResizeNotification object:self.window];
}
TL;DR
Look for the method - (void)setSelectedViewController:(NSViewController <MASPreferencesViewController> *)controller inside MASPreferencesWindowController.m
Comment this line:
// Record new selected controller in user defaults
[[NSUserDefaults standardUserDefaults] setObject:controller.identifier forKey:kMASPreferencesSelectedViewKey];
Now change the way you initialise your NSWindowController so you create a new instance every time, otherwise it will still remember the last selected tab:
-(NSWindowController *)preferencesWindowController
{
NSViewController *generalViewController = [[GeneralPreferencesViewController alloc] init];
NSViewController *accountViewController = [[AccountPreferencesViewController alloc] init];
NSViewController *troubleshootingViewController = [[TroubleShootingPreferencesViewController alloc] init];
//NSArray *controllers = [[NSArray alloc] initWithObjects:generalViewController, accountViewController, troubleshootingViewController, nil];
NSArray *controllers = [[NSArray alloc] initWithObjects:accountViewController, generalViewController, troubleshootingViewController, nil];
// To add a flexible space between General and Advanced preference panes insert [NSNull null]:
// NSArray *controllers = [[NSArray alloc] initWithObjects:generalViewController, [NSNull null], advancedViewController, nil];
NSString *title = NSLocalizedString(#"Preferences", #"Common title for Preferences window");
_preferencesWindowController = [[MASPreferencesWindowController alloc] initWithViewControllers:controllers title:title];
return _preferencesWindowController;
}
I have a table and so far, I can populate it by adding values to an array in the code. But I want to use a textfield values and enter it there, the only problem is, if I do that I can only have one value, I want to pass a textfield value without overwriting the current cell.
Here is what I have:
-(void)viewDidLoad
{
[super viewDidLoad];
//[self fetchRecords];
titlestring = [[NSUserDefaults standardUserDefaults] objectForKey:#"titletext"];
detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:#"details"];
tabledata = [[NSArray alloc] initWithObjects:#"titlestring", nil];
tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];
}
Is there a way to use MyArray[i] = marry initWithObjectcs...
Thanks.
I don't understand what you is your problem.
But if you want to add objects to an existing array, you should use a NSMutableArray not a NSArray.
There are many methods in the SDK that ask for a list of strings, terminated by a nil, for example, in UIActionSheet:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
'otherButtonTitles' in this case is a list of NSStrings terminated with a nil. What I'd like to do is call this method with a constructed NSMutableArray of NSStrings, because I'd like to create and order the arguments dynamically. How would I do this? I'm not sure how to create a nil-terminated pointer to NSStrings in this case, and if passing it in would even work. Do I have to alloc the memory for it manually and release it?
You cannot convert any array into a variadic list.
However, for UIActionSheet, you could add those otherButtonTitles after the sheet is created, using -addButtonWithTitle:
UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:...
/*etc*/
otherButtonTitles:nil];
for (NSString* otherButtonTitle in otherButtonTitlesArray)
{
[sheet addButtonWithTitle:otherButtonTitle];
}
I need to make a dynamic action sheet as well. So I made a mostly empty action sheet. Added my buttons. Then added the Cancel button and marked it as cancel.
sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:homeName, nil];
[sheet addButtonWithTitle:otherButton1];
[sheet addButtonWithTitle:otherButton2];
[sheet addButtonWithTitle:#"Cancel"];
[sheet setCancelButtonIndex:[sheet numberOfButtons] - 1];
sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[sheet showInView:self.view];
[sheet release];
You sure CAN convert NSArray to va_list. For example to use with NSString
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList
Like this:
+ (id)stringWithFormat:(NSString *)format array:(NSArray *)arguments;
{
NSRange range = NSMakeRange(0, [arguments count]);
NSMutableData *data = [NSMutableData dataWithLength:sizeof(id) * [arguments count]];
[arguments getObjects:(__unsafe_unretained id *) data.mutableBytes range:range];
return [[NSString alloc] initWithFormat:format arguments:data.mutableBytes];
}
i want to set the name of an object like UIButton from a string.
NSString *buttonName = [[NSString alloc] initWithString:#"someString"];
My goal is:
UIButton *someString = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
how can i solve this?
You can't - variable names are resolved by the compiler well before any Objective-C code is executed. What you can do is maintain a map of strings to objects like buttons etc. using NSMutableDictionary:
NSString *string = #"someString";
[buttonMap setObject: [UIButton buttonWithType:UIButtonTypeCustom] forKey: string];
//...time passes...
[[buttonMap objectForKey: #"someString"] setEnabled: YES];
I'm actually starting to loose the will to live, this piece of code is driving me nuts!
I'm trying to get the content of mathspractice.txt into *myLabel
I'm using an array which is:
-(void)loadText
{
NSArray *wordListArray = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#”mathspractice” ofType:#”txt”]
encoding:NSMacOSRomanStringEncoding error:NULL] componentsSeparatedByString:#”\n”]];
self.theMathsPractice = wordListArray;
[wordListArray release];
}
and then I'm trying to pass it into *myLabel
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,100,960,40)];
myLabel.text = *theMathsPractice;
[myScrollView addSubview:myLabel];
[myLabel release];
}
Can anyone help?
It looks on quick inspection that your theMathsPractice is an NSArray, not an NSString, which is what you'd want to assign to the label's text property. You should at least format that array back into a string of some sort before assigning it to the label.
(Also not sure why you're dereferencing it with the * in the assignment-- I would think that would throw a compiler error, since naked non-reference Objective-C objects are not really allowed.)
I would use the following:
myLable.text = [theMathsPractice componentsJoinedByString:#" "]);