How to force an event? [closed] - iphone

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem with some control i downloaded, i want to change the background when the button is pressed, but it seems that when the button is pressed the app will not listen to any event until the current event is finished here is my code
-(void) menuItem:(RRCircularItem *)item didChangeActive:(BOOL)active {
[menu progress];
[menu setBackgroundColor1:[UIColor colorWithPatternImage:[UIImage imageNamed:#"menuabiertoloadingretina.png"]]];
[menu hideWithAnimationBlock:^{
self.view.backgroundColor = [UIColor whiteColor];
}];
[menu release], menu = nil;
self.viewCarga.hidden = NO;
[menu setBackgroundColor1:[UIColor colorWithPatternImage:[UIImage imageNamed:#"menuabiertoloadingretina.png"]]];
NSLog(#"Item %# did change state to %d", item.text, active);
if ([item.text isEqualToString:#"EDITORIALES"]){
ViewController *viewControler = [[ViewController alloc] initWithNibName:#"EditorialesViewController" bundle:nil];
[self presentViewController:viewControler animated:YES completion:nil];
[menu setBackgroundColor1:[UIColor colorWithPatternImage:[UIImage imageNamed:#"menuabiertoloadingretina.png"]]];
}else if ([item.text isEqualToString:#"GOLES"]){
GolViewController *viewControler = [[GolViewController alloc] initWithNibName:#"GolViewController" bundle:nil];
[self presentViewController:viewControler animated:YES completion:nil];
}else if ([item.text isEqualToString:#"TABLA"]){
WebViewController *viewControler = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
[self presentViewController:viewControler animated:YES completion:nil];
}else if ([item.text isEqualToString:#"AUNLI"]){
AUNLIViewController *viewControler = [[AUNLIViewController alloc] initWithNibName:#"AUNLIViewController" bundle:nil];
[self presentViewController:viewControler animated:YES completion:nil];
}else if ([item.text isEqualToString:#"HORARIOS"]){
HorariosViewController *viewControler = [[HorariosViewController alloc] initWithNibName:#"HorariosViewController" bundle:nil];
[self presentViewController:viewControler animated:YES completion:nil];
}
if (active && ![menu isLabelActive]) {
[menu setLabelActive:YES];
[menu setSliderValue:1];
} else if (!active && [menu isLabelActive]) {
BOOL hasActive = NO;
for (int i = 0; i < 6; i++) hasActive |= [menu isItemActive:i];
if (!hasActive) {
[menu setLabelActive:NO];
[menu setSliderValue:0 animated:NO];
}
}
}
i want to use this event [menu progress]; before everything, but it is not working.

if i understand Well, you press some button and you stay lock.Its because you are doing everything in main thread. Thats why you need to use threads, this way you will do multi sync/async processes preventing locks in your app. You can use NSThread, dispatch or NSOperationQueue...I like to use
GCD(Grand Central Dispatch)
Sample:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
dispatch_async(dispatch_get_main_queue(), ^(void){
//User Interface Updates
});
});

Related

UIMenuController doesn't show in a second UIViewController

I have two UIViewController, one is the main and from this trough a button you can go to the second. In SecondView.m I have the following code:
- (IBAction)showpopup:(id)sender {
[self becomeFirstResponder];
UIMenuController *sharedController = [UIMenuController sharedMenuController];
UIMenuItem *x2 = [[UIMenuItem alloc] initWithTitle:#"2x2" action: #selector(mat)];
UIMenuItem *x3 = [[UIMenuItem alloc] initWithTitle:#"3x3" action: #selector(mat)];
UIMenuItem *x4 = [[UIMenuItem alloc] initWithTitle:#"4x4" action: #selector(mat)];
UIMenuItem *x5 = [[UIMenuItem alloc] initWithTitle:#"5x5" action: #selector(mat)];
NSArray *menuArray = [NSArray arrayWithObjects: x2,x3,x4,x5, nil];
CGRect drawRect = [sender convertRect:[sender bounds] toView: self.view];
[sharedController setTargetRect:drawRect inView: self.view];
[sharedController setMenuItems:menuArray];
[sharedController setMenuVisible:YES animated:YES];
[sharedController setMenuItems: nil];
}
-(BOOL)canBecomeFirstResponder{
return YES;
}
-(int)mat:(id)sender{
return 0;
}
The Button is linked as "touch up inside", but when I run the UIMenuController doesn't show up.
The exact same code works in the main UIViewController.
Thanks
If I am not missing anything, I think you should, e.g., add your sharedController.view as a subview to your mainController.view, e.g. (assuming that `showpopup is defined in your main controller):
- (IBAction)showpopup:(id)sender {
[self becomeFirstResponder];
UIMenuController *sharedController = [UIMenuController sharedMenuController];
...
[sharedController setMenuItems:menuArray];
[sharedController setMenuVisible:YES animated:YES];
[sharedController setMenuItems: nil];
[self.view addSubview:sharedController.view];
}
or you could present modally your sharedController (replace the addSubview line above):
[self presentViewController:sharedController animated:YES completion:nil];
In any case, it seems to me that the "presenting" bit is missing.

multi button tag multi ple view

On click each button showing subview view contains learn and play option.While clicking , want to show the different view depends on the uibutton selection.Here my code .
-(IBAction)animals
{
CGPoint point = [tap locationInView:self.animalsbut];
pv = [PopoverView showPopoverAtPoint:point inView:animalsbut withContentView:alertvu delegate:self];
pv.tag=1;
}
-(IBAction)birds
{
CGPoint point = [tap locationInView:self.birdsbut];
pv = [PopoverView showPopoverAtPoint:point inView:birdsbut withContentView:alertvu delegate:self];
pv.tag=2;
//[self checkingme:pv.tag];
}
-(IBAction)direct
{
CGPoint point = [tap locationInView:self.direcbut];
pv = [PopoverView showPopoverAtPoint:point inView:direcbut withContentView:alertvu delegate:self];
pv.tag=4;
}
-(IBAction)fruit
{
CGPoint point = [tap locationInView:self.fruitbut];
pv = [PopoverView showPopoverAtPoint:point inView:fruitbut withContentView:alertvu delegate:self];
pv.tag=3;
}
method
-(IBAction)check:(NSInteger)sender
{
UIButton *mybutton =(UIButton*) [self.view viewWithTag:sender];
if(pv.tag==1)
{
NSLog(#"button log%d",mybutton.tag);
if(mybutton.tag==0)
{
animallearn *aview=[[animallearn alloc]initWithNibName:#"animallearn" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
else //(mybutton.tag==1)
{
playview *pview=[[playview alloc]initWithNibName:#"playview" bundle:nil];
[self.navigationController pushViewController:pview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
}
else if(pv.tag==2)
{
if(mybutton.tag==0)
{
birdview *aview=[[birdview alloc]initWithNibName:#"birdview" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
else if(mybutton.tag==1)
{
playview *pview=[[playview alloc]initWithNibName:#"playview" bundle:nil];
[self.navigationController pushViewController:pview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
}
else if(pv.tag==3)
{
if(mybutton.tag==0)
{
fruitview *aview=[[fruitview alloc]initWithNibName:#"fruitview" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
else if(mybutton.tag==1)
{
playview *pview=[[playview alloc]initWithNibName:#"playview" bundle:nil];
[self.navigationController pushViewController:pview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
}
else if(pv.tag==4)
{
if(mybutton.tag==0)
{
directview *aview=[[directview alloc]initWithNibName:#"directview" bundle:nil];
[self.navigationController pushViewController:aview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
else if(mybutton.tag==1)
{
playview *pview=[[playview alloc]initWithNibName:#"playview" bundle:nil];
[self.navigationController pushViewController:pview animated:YES];
[pv performSelector:#selector(dismiss) withObject:nil afterDelay:0.1f];
}
}
else
{
return ;
}
}
Here problem is whenever i second button it showing the button tag 0 view only in all the cases?Can any one help me to sort it out
You should also set the tags of UIButtons/UIViews you want to access via tag. like secondButton.tag = 3; and the sender argument passed is not of type NSInteger it is of UIView, so change it like this! This is should solve your problem!
-(IBAction)check:(id)sender
{
UIButton *mybutton =(UIButton*) sender;
// continue whatever you were doing earlier!
}

UISwipeGestureRecognizer making app crash changing screens?

I have a simple 3 page app, I can go from page 1 to page 2 swiping left.
However, when I am on page 2, I need to add the ability to go to either page 1 swiping right, or page 3 swiping left, but it just keeps crashing.
I suspect it may be a memory release issue, but I wonder if you could be so kind as to check the code below for any glaring errors?
Many thanks,
- (void)viewDidLoad {
UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:swipeLeftRight];
[UISwipeGestureRecognizer release];
}
- (IBAction)swipeLeftDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}
}
- (IBAction)swipeRightDetected:(UISwipeGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
EDIT:
You need to implement a method called handleGesture: since that's the action you're passing in to the gesture recognizer.
Fixed, I found an older post by a person called rptwsthi, I used some of that to fix it;
Changed the viewDidLoad {
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
And using this to switch the pages;
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
[Page2ViewController release];
}else{
ViewController *UIViewController =
[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
ViewController *VC = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[Page2ViewController release];
}
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}else{
Page3ViewController *UIViewController =
[[Page3ViewController alloc] initWithNibName:#"Page3ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}
}
Not sure how neat it is in a true coders view, but it works, sometimes you just gotta go with what works right! :)

Black screen appears, randomly after taking an image and tapping 'USE' in the UIImagePickerController

I have an app that's almost done and now while it was under QA, the QA engineer came across a random issue in which a black screen appears after tapping on USE in UIImagePickerController view. Further in didFinishPickingMediaWithInfo the image is being saved for future refrencing and is being shown in an UIImageView, I am also using camera overlay for adding a button to the UIImagePickerView and the max memory usage of the app doen't goes beyond 13 MB. The issue is not arising when switching to Camera roll mode from capture mode. Also in the same view iADs are being presented. Underneath is the code, to which the issue may concern, also images are being attached to get an idea of the issue. Any help would be greatly appreciated.
Thanks in advance.
(void) launchCamera
{
// Set up the camera\
CustomCameraView *cameraController = [[CustomCameraView alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;
cameraController.showsCameraControls = YES;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;
// overlay on top of camera lens view
UIButton *cameraRoll = [[UIButton alloc] initWithFrame:CGRectMake(15, 380, 40, 40)];
[cameraRoll setAlpha:0.0f];
[cameraRoll setBackgroundImage:[UIImage imageNamed:#"CamerRoll_New"] forState:UIControlStateNormal];
[cameraRoll addTarget:self action:#selector(switchToCameraRoll) forControlEvents:UIControlEventTouchUpInside];
cameraController.cameraOverlayView = cameraRoll;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:1.5f];
cameraRoll.alpha = 1.0f;
[UIView commitAnimations];
[self presentModalViewController:cameraController animated:YES];
}
-(void)switchToCameraRoll
{
DLog(#"Camera Roll");
[self dismissViewControllerAnimated:NO completion:^(void){ [self photoSourcePhotoAlbum];}];
}
-(void) photoSourcePhotoAlbum
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.view addSubview:progressView];
timer = [NSTimer scheduledTimerWithTimeInterval:.017 target:self selector:#selector(progressChange) userInfo:nil repeats:YES];
[picker dismissModalViewControllerAnimated:YES];
[self performSelectorInBackground:#selector(saveImage:) withObject:info];
[self performSelector:#selector(navigateToEditView) withObject:nil afterDelay:2.1];
}
-(void)navigateToEditView
{
[self performSegueWithIdentifier:#"presentRDetailModalViewController" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum)
{
[picker dismissViewControllerAnimated:NO completion:^(void){ [self photoSourceCamera];}];
}
else
{
[picker dismissModalViewControllerAnimated:YES];
}
}
-(void)saveImage:(NSDictionary *)info
{
NSString *imageName = [[[DataStaging dataStaging] getGUID] stringByAppendingString:#".jpg"];
rImageName = imageName;
UIImage *rImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Compress image
[[FileOperations fileOperations] PersistData:UIImageJPEGRepresentation(rImage, 0.4) name:imageName];
DLog(#"%#",[[FileOperations fileOperations] fetchPath:imageName]);
//Actual image taken
[[self rImageView] setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
if ([rImageName length] == 0)
{
[[self rDetails] setHidden:YES];
[[self trashRImage] setHidden:YES];
}
else
{
[[self rDetails] setHidden:NO];
[[self trashRImage] setHidden:NO];
}
[progressView removeFromSuperview];
}
}
Make sure you don't call presentModalViewController:animated: twice before dismissModalViewControllerAnimated:
It helped in my case.
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
if (_imagePickerController!=nil || _imagePopoverController!=nil) {
return;
}
_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
_imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
_imagePickerController.sourceType = sourceType;
_imagePickerController.delegate = self;
switch (sourceType) {
case UIImagePickerControllerSourceTypeCamera:
_imagePickerController.showsCameraControls = YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];
break;
default:
_imagePopoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePickerController];
_imagePopoverController.delegate = self;
CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.y = rect.size.height - 70.0;
rect.size.height -= rect.origin.y;
[_imagePopoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
break;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImageWriteToSavedPhotosAlbum([info objectForKey:UIImagePickerControllerOriginalImage],nil,nil,nil);
_currentImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}

when UISegmentcontrol item changing, why it will hide customBadge in IOS 5?

I have added custom Badge on UISegmentControl. In Xcode 4.0.2 SDK 4.3 its working fine. But in Xcode 4.2 SDK 5.0 custom badge is not shown when another item is selected. I dont know why this happens? I'm using following code for IOS 4.3 Xcode 4.0.2:
- (void)viewDidLoad {
[super viewDidLoad];
super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"wallpaper.png"]];
NSLog(#"Root View Loaded");
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Home",#"Surveys",#"Results",#"Create",#"Settings",
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 310, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor darkGrayColor];
segmentedControl.momentary = NO;
segmentedControl.highlighted = YES;
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
NSArray *segmentBarArray = [NSArray arrayWithObjects:
segmentBarItem,nil];
[[self appDelegate] setSegmentedControl:segmentedControl];
[[self appDelegate] setSegmentBarArray:segmentBarArray];
[self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:YES];
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
int surveycount = [[self appDelegate] getUnreadSurveyCount];
surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:#"%d",surveycount]];
[surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];
[segmentedControl addSubview:surveyCountBadge];
if (surveycount == 0) {
[surveyCountBadge setHidden:YES];
}else{
[surveyCountBadge setHidden:NO];
}
}
-(void)segmentAction:(id) sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"selected index is %d",segmentedControl.selectedSegmentIndex);
if (segmentedControl.selectedSegmentIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if(segmentedControl.selectedSegmentIndex ==1){
surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:#"SurveyForMeView" bundle:nil];
[self.navigationController pushViewController:surveyForMeViewController animated:YES];
} else if(segmentedControl.selectedSegmentIndex ==2){
resultViewController = [[ResultView alloc] initWithNibName:#"ResultView" bundle:nil];
[self.navigationController pushViewController:resultViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==3) {
newSurveyViewController = [[NewSurveyView alloc] initWithNibName:#"NewSurveyView" bundle:nil];
[self.navigationController pushViewController:newSurveyViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==4) {
settingsViewControlle = [[SettingsView alloc] initWithNibName:#"SettingsView" bundle:nil];
[self.navigationController pushViewController:settingsViewControlle animated:YES];
}
}
Any thing else should I mention in this?
You are adding a subview to a segmentedControl. As you don't know how the system redraws the control when another item is selected, you cannot say if your view will be removed or not. Let the control, button or segmentedControl do its work and if you want to add a badge, add it as a subview of its parent.
[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
-10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width,
surveyCountBadge.frame.size.height)];
[[segmentedControl superView] addSubview: surveyCountBadge];