Removeallobjects in array from another view? Xcode iOS - xcode

Please forgive me is this is too vague. I have an app that has populated arrays based on where the user browses to. One is just all sites visited and is accessible in another viewcontroller and another is all text input in to the textview which is displayed while the user types. I don't know if any of that is important but my question is, I would like to have a settings page that has the option to clear that data. I can do it from the header file in that view its self but not sure how to send the removeallobjects command to other views or arrays.
In each view I have a button to call this:
-(IBAction)clearPreText {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"clear all predictive typing?"
message:#"press ok to clear"
delegate: self
cancelButtonTitle:#"cancel"
otherButtonTitles:#"ok", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[pastUrls removeAllObjects];
[[NSUserDefaults standardUserDefaults] setObject:autocompleteUrls forKey:#"PastUrls"];
[self.autocompleteTableView reloadData];
}}
I would like to place those buttons in a new view, a settings page.
Thanks for anything.

Both controllers will have access to the app delegate and the app delegate will have access to each of your controllers. When I need to do something like this, I usually do it through the app delegate.

Related

Parse - No placeholder and field email doesn't work in SignUpViewController

I have a problem in Parse with my signUpView.
When I try with LogInAndSignUpDemo code (the Parse tutorial) it works well.
For my project, I have a button "log in" in my view, when the user selects "log in", I open the LogInviewController. If the user doesn't have an account, he can "sign up".
When he sign up, he has this view : Image of mysignupview
My problem is on this view because I don't have placeholder for password and email fields, and I can't select the email field.
My code :
-(IBAction) bouton_log_in_action:(id)sender
{
if (![PFUser currentUser]) { // No user logged in
// Create the log in view controller
PFLogInViewController *logInViewController = [[PFLogInViewController alloc] init];
[logInViewController setDelegate:self]; // Set ourselves as the delegate
// Create the sign up view controller
PFSignUpViewController *signUpViewController = [[PFSignUpViewController alloc] init];
[signUpViewController setDelegate:self]; // Set ourselves as the delegate
// Assign our sign up controller to be displayed from the login controller
[logInViewController setSignUpController:signUpViewController];
// Present the log in view controller
[self presentViewController:logInViewController animated:YES completion:NULL];
}
}
I don't understand where is the problem, because I use the code I found on Parse documentation...
I found the problem.
It's a bug, it'll be fix in 1.2.21 release...

How to configure content for NSPopUpButton

I have a NSPopUpButton configured with bindings and coredata. Everything is working perfectly, however I would like to add a item that implements an action to "edit the list", like
Item 1
Item 2
Item 3
Item 4
------
Edit List..
Is this Possible to do with Bindings?
I think that the answer is NO, at least not completely. I thought I would provide the content to the button programatically and maintain bindings for the Selected Value , so this is what I came up with
- (void)updateSectorPopupItems
{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:#"Sector"];
NSSortDescriptor *sortPosition = [[NSSortDescriptor alloc] initWithKey:#"position" ascending:YES];
[request setSortDescriptors:#[sortPosition]];
NSError *anyError = nil;
NSArray *fetchObjects = [_gdcManagedObjectContext executeFetchRequest:request
error:&anyError];
if (fetchObjects == nil) {
DLog(#"Error:%#", [anyError localizedDescription]);
}
NSMutableArray *sectorNames = [NSMutableArray array];
for (NSManagedObject *sector in fetchObjects) {
[sectorNames addObject:[sector valueForKey:#"sectorCatagory"]];
}
[_sectorPopUpBotton addItemsWithTitles:sectorNames];
NSInteger items = [[_sectorPopUpBotton menu] numberOfItems];
if (![[_sectorPopUpBotton menu] itemWithTag:1] ) {
NSMenuItem *editList = [[NSMenuItem alloc] initWithTitle:#"Edit List..." action:#selector(showSectorWindow:) keyEquivalent:#""];
[editList setTarget:self];
[editList setTag:1];
[[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
}
A couple of problems I'm having with this
1) When adding the Menu Item using
[_sectorPopUpBotton menu] insertItem:editList atIndex:items];
no matter what value is entered in atIndex, the item always appears at the top of the Menu list.
2) I just want the "Edit List..." menuitem to initiate the action, how do I prevent this from being selected as a value?
You might as well do that using an NSMenuDelegate method.
Actually in this way you can also keep the bindings for getting the NSPopUpButton content objects (in your case from the NSArrayController bound to the CoreData stack).
1) Set an object as delegate for the NSPopUpButton internal menu, you can do that in the Interface Builder by drilling down the NSPopUpButton to reveal its internal menu. Select it and then set its delegate in the Connections Inspector panel to the object you have designated to this task. As such delegate you might for example provide the same ViewController object which manages the view where the NSPopUpButton exists.
You'll then need to have the object provided as delegate adhere to the NSMenuDelegate informal protocol.
2) Implement the NSMenuDelegate method menuNeedsUpdate: there you'll add the NSmenuItem(s) (and eventually separators) you want to provide in addition to those already fetched by the NSPopButton's bindings.
An example code would be:
#pragma mark NSMenuDelegate
- (void)menuNeedsUpdate:(NSMenu *)menu {
if ([_thePopUpButton menu] == menu && ![[menu itemArray] containsObject:_editMenuItem]) {
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:_editMenuItem];
}
}
In this example the _editMenuItem is an NSMenuItem property provided by the object implementing this NSMenuDelegate method. Eventually it could be something as this:
_editMenuItem = [[NSMenuItem alloc] initWithTitle:#"Edit…" action:#selector(openEditPopUpMenuVC:) keyEquivalent:#""];
// Eventually also set the target for the action: where the selector is implemented.
_editMenuItem.target = self;
You'll then implement the method openEditPopUpMenuVC: to present to the user the view responsible for editing the content of the popUpButton (in your case the CoreData objects provided via bindings).
The only problem I haven't yet solved with this approach is that when getting back from the view where the edit happens, the NSPopUpButton will have the new item "Edit…" selected, rather than another "valid" one, which is very inconvenient.

UIAlertview delegate will crash when setting delegate to self

I have this simple piece of code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Contact" message:#"This contact does not exist yet" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:#"Not now", nil];
[alert show];
If I set delegate to 'nil', everything is fine. However, if I set delegate to 'self' and add either a clickedButtonAtIndex or didDismissWithButtonIndex delegate, the application crashes with EXC_BAD_ACCESS
I think you did not set the alertView delegate method.
First set the alertView delegate protocol in .h file.
#interface MainViewController : UIViewController<UIAlertViewDelegate>
Then implement this method, it will work fine
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
break;
case 1:
break;
default:
break;
}
}
your problem is that your object (self) doesn't exists anymore! but the alertview try to access it, so your get EXC_BAD_ACCESS. Check if your delegate object (self) is alive!
The problem was due to my flow. I have a class calling a URL. the Viewcontroller did in fact finish long before the response came form the server. I therefore had to implement an NSRunLoop in the caller in order to wait for the server communication to finish. Based on some ExitCode form the called routine, I could then only display an alert and get the delegate to handle the pressed button. Thanks anyway to Chakalaka for putting me on the track.

Recognising the titleLabel of a button in Xcode

I am writing an iPad app that has a Cancel button on a view controller. When the work on the controller is finished the labelText changes from "Cancel" to "Done". Therefore I only want the alert to show when the labelTitle is still "Cancel". If it is "Done" I just want the controller to dismiss. Here is the code:
NSLog(#"%#",closeButton.titleLabel.text);
if (closeButton.titleLabel.text = #"Cancel")
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"i-observe"
message:#"Are you sure that you want to cancel?" delegate:self cancelButtonTitle:#"no" otherButtonTitles:#"yes", nil];
[alert show];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
}
What happens is that the alert appears in both cases ("Cancel" and "Done"). However in the NSLog it shows that the titleLabel has changed. Any ideas?
First of all, you are setting the text to "Cancel" within your if statement. So, your if statement logic says "if the title text is successfully set to 'Cancel', then display the alert." The comparison operator is ==, not =.
Second, you do not want to compare two different text objects with the == operator. You want to use isEqualToString: like this:
if ([closeButton.titleLabel.text isEqualToString:#"Cancel"]) { ... }
You can't use "==" to compare strings. You use isEqualToString, like this:
if ([closeButton.titleLabel.text isEqualToString:#"Cancel"])

How do I put the name of the IBAction pressed on my main view controller in a NSString on my modal view?

I have about ten IBActions on my main view that go to the same modal view and I need to know how to see what IBActon was pressed to get there and put that in a NSString on the modal view.
Here's the code that sends you to the modal view. The only difference in the IBActions is the name. Like playMovie: and playMovie2: and so on.
-(IBAction)playMovie:(id)sender{
VideoSubViewController *subView = [[[VideoSubViewController alloc] init] autorelease];
[subView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:subView animated:YES];}
You could create an init* on the modal view that takes that information. The usage would be:
VideoSubViewController *subView =
[[[VideoSubViewController alloc] initForAction:#"playMovie"] autorelease];
You then store that information in the view instance in order to use it.

Resources