Hello i have a nib file which contains 3 UIButtons , i need to change the attributes of button at runtime after loading from nib but i don't know what am i doing wrong. I have also NSlog after changing the booleans but i always get '0' in output. Any help thx
-(void)SettingBtnPressed:(id)sender
{
UIButton *btn =(UIButton*)sender;
UITableViewCell *btncell =(UITableViewCell *)[btn superview];
if ([[recentActivities objectAtIndex:btn.tag] isKindOfClass:[MeetingSummary class]]) {
[MainManager getSharedInstance].Summary = [recentActivities objectAtIndex:btn.tag];
MeetingSummary *ms=[recentActivities objectAtIndex:btn.tag];
Meeting *m=ms.meeting;
if(![popoverController isPopoverVisible]){
lastPoint = sender;
if (![m.endedDate isEqualToString:#"1/1/0001"]) {
if (settingpop) {
[settingpop release];
}
settingpop = [[SettingsPopViewController alloc] initWithNibName:#"SettingsPopViewController" bundle:nil];
settingpop.start.hidden=YES;
settingpop.start.enabled=NO;
NSLog(#"%i",settingpop.start.hidden);
NSLog(#"%i",settingpop.start.enabled);
settingpop.btnShowDetail.enabled=NO;
settingpop.btnShowDetail.hidden=YES;
// [settingpop.start removeFromSuperview];
// [settingpop.btnShowDetail removeFromSuperview];
settingpop.view.frame=CGRectMake(0, 0, 250, 54);
}
else
{
settingpop = [[SettingsPopViewController alloc] initWithNibName:#"SettingsPopViewController" bundle:nil];
}
settingpop.delegate = self;
[settingpop setActivityView:self.view];
popoverController = [[[UIPopoverController alloc] initWithContentViewController:settingpop] retain];
[popoverController setPopoverContentSize:CGSizeMake(250.0f, 162.0f)];
[popoverController presentPopoverFromRect:btn.frame inView:btncell
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
[popoverController dismissPopoverAnimated:YES];
}
}
}
Move the row:
settingpop.view.frame=CGRectMake(0, 0, 250, 54);
before the row:
settingpop.start.hidden=YES;
Are you talking about adjusting the properties of the start and btnShowDetail controls of SettingsPopViewController? You cannot access its controls immediately after the initWithNibName. You have to wait until after the view has been created, i.e., wait until after viewDidLoad is called, because the NIB may well not have been loaded until then.
Related
I want to touch on UIImageView and that image display in another view in full size using navigate the view. same as gallery in mobile.
just creating demo programmatically. because I'm new in iphone developing.
Here is my code.
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[imageView setImage:[UIImage imageNamed:#"Motocross.png"]];
[imageView setUserInteractionEnabled:YES];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[imageView setTag:k];
[scroll addSubview:imageView];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Enter in touchesBeganWithEvent method");
UITouch *touch = [touches anyObject];
if ([touch view] == imageView)
{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
NSLog(#"Exit from if condition of touch");
}
NSLog(#"Exit from touchesBeganWithEvent method");
}
I want to tap on imageView and view will navigate to detailViewController and display that image in big size. but this touchesBegan method not called when i tap on imageView. In this i'm not creating array of UIImageView just alloc init in loop.. Thank you!
Rather than using touchesBegan: on the view, consider adding a UITapGestureRecognizer to each image view. When the image view is tapped (you are setting userInteractionEnabled already which is good) then you can get the view that was tapped with sender.view.
Note that you create a new instance of UITapGestureRecognizer for each image view that you have, but that the target/action of each gesture can be the same.
The problem with your current code is that this check:
if ([touch view] == imageView)
verifies the touch only against the last image view that you added to the scroll view. So, touching the last one should work, but all the others won't.
As per the comment from #vin, the correct solution to that issue is to verify the class type of the view that was touched:
if ([[touch view] isKindOfClass:[UIImageView class]]) {
(though the scroll view may have other image view subclasses than just the ones you add...)
Try bellow code or its better to use Collection view
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
UIButton *buttonForImage=[[UIButton alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[buttonForImage setImage:[UIImage imageNamed:#"Motocross.png"] forState:UIControlStateNormal];
[buttonForImage addTarget:self action:#selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[buttonForImage setTag:k];
[scroll addSubview:buttonForImage];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)imageButtonPressed:(UIButton*)sender{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = sender.imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
You need to use UICollectionview rather than trying to juggle with rows /colns, images touches etc...
Good tutorial is here... UICollectionView Tutorial
(Assuming you dont support below iOS 6.0)
In the prototype cell as no IBOutlet can be given so I got a handle on my UIbutton with a tag. But when setting the image for the button in cellForRowAtIndexPath method its not showing the correct image for enable and disabled state.
in cellForRowIndexPath:
if([cellIdentifier isEqualToString:CELL_ID_COMPLIANCE])
_infoButton = (UIButton *)[cell viewWithTag:800];
else
_infoButton = (UIButton *)[cell viewWithTag:900];
if([[comments objectAtIndex:kEntitlementComment] length] > 0){
_infoButton.enabled= YES;
[_infoButton setImage:[UIImage imageNamed:#"icon_info.png"] forState:UIControlStateNormal];
TRACELOG(#"the coments :%#", [comments objectAtIndex:kEntitlementComment]);
}
else{
_infoButton.enabled= NO;
[_infoButton setImage:[UIImage imageNamed:#"info_icon_disabled.png"] forState:UIControlStateDisabled];
}
I have two kind of prototype cells in the table but the button images are only of two kinds. I also tried
[self performSelectorOnMainThread:#selector(doButtonSettings:)
with
Object:textArray waitUntilDone:YES];
in the selector I am setting the images for the button. As you know the buttonType is a private setter so cant be changed dynamically. How do i make button images consistent with the conditions?
Why don't you use the same tag for the buttons?
//All prototype cells info buttons have the same tag
_infoButton = (UIButton *)[cell viewWithTag:900];
if([[comments objectAtIndex:kEntitlementComment] length] > 0){
_infoButton.enabled= YES;
[_infoButton setImage:[UIImage imageNamed:#"icon_info.png"] forState:UIControlStateNormal];
TRACELOG(#"the coments :%#", [comments objectAtIndex:kEntitlementComment]);
}
else{
_infoButton.enabled= NO;
[_infoButton setImage:[UIImage imageNamed:#"info_icon_disabled.png"] forState:UIControlStateDisabled];
}
Move the calls to setImage to where the cell is created. Set the images for both the normal AND disabled states for ALL buttons.
The in your if statement you only need to toggle the buttons state
if([[comments objectAtIndex:kEntitlementComment] length] > 0){
_infoButton.enabled= YES;
}
else{
_infoButton.enabled= NO;
}
But this change alone should not fix your issue. It would be helpful if you would post all the code within cellForRowIndexPath. I suspect that the issue is above where you call dequeueReusableCellWithIdentifier and then if nil create the cell.
I got the soln:
Actually in the IBAction method i had popover tied to my infoButton.
- (IBAction)infoButtonAction:(id)sender event:(id) event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_summaryTableView];
NSIndexPath *indexPath = [_summaryTableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [_summaryTableView cellForRowAtIndexPath:indexPath];
_infoButton.tag=indexPath.row;
if ( [self.myPopoverController isPopoverVisible] )
{
// Close the popover view if toolbar button was touched again and popover is already visible
[self.myPopoverController dismissPopoverAnimated: YES];
return;
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"iPad" bundle: nil];
NSITSLMEntitlementSummCommentsViewController *popoverContent = [storyboard instantiateViewControllerWithIdentifier: #"slm_ES_Comments"];
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController: popoverContent];
popoverContent.comments.text = [[self.dataController getSummaryRecordAtIndex:indexPath.row] objectAtIndex:kEntitlementComment];
// Present the popover view non-modally with a refrence to the toolbar button which was pressed
if([popoverContent.comments.text length] > 0)
[self.myPopoverController presentPopoverFromRect:[sender frame] inView:cell permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
_infoButton.tag= 800;
}
The info button tag was changing whenever i was pressing any of the buttons in the table. So i added the last line and now its fine. Thanx anyways..
When I click on a image I want to show another popover, but it doesn't work!
My handle Tap method looks like :
-(void)handleTapView:(UITapGestureRecognizer*)recognizer
{
CGPoint startPoint = [recognizer locationInView:recognizer.view];
NSLog(#"handle Tap VIEW!!!!!!!!");
if ([recognizer.view isKindOfClass:[UIImageView class]] ) {
NSLog(#"Tap Image!!!!!!!!");
}
else if ([self.popover isPopoverVisible]) {
[self.popover dismissPopoverAnimated:YES];
}
else {
ShapesListViewController *shapes = (ShapesListViewController*) [self.storyboard instantiateViewControllerWithIdentifier:#"ShapesListViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shapes];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:nav];
shapes.delegate = self;
self.popover = pop;
CGRect popoverRect;
popoverRect.origin = startPoint;
popoverRect.size.width = 1;
popoverRect.size.height =1;
[pop presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
In viewdidload :
ImageView *imgv = [[ImageView alloc] initWithImage:[UIImage imagNamed:#"delete.png"]];
imgv.center = CGPointMake(250,250);
[self.view addSubview:imgv];
ImageView *imgv2 = [[ImageView alloc] initWithImage:[UIImage imageNamed:#"gear.png"]];
imgv2.center = CGPointMake(400,400);
[self.view addSubview:imgv2];
//Tap Recognizer
self.singelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapView:)];
[self.singelTapGestureRecognizer setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:self.singelTapGestureRecognizer];
You need to attach a separate gestureRecogniser to each view whose gestures you want to capture - in your case, both of your imageViews.
UITapGestureRecognizer *tapGR1, *tapGR2;
SEL selector = #selector(handleTapView:);
tapGR1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
tapGR2 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selector];
[imgv1 addGestureRecognizer:tapGR1];
[imgv2 addGestureRecognizer:tapGR2];
Don't attach a tapGR to their superview.
Then you also need to set userInteractionEnabled on each of the imageViews otherwise they will ignore touches (UIImageView defaults to userInteractionEnabled = NO):
imgv1.userInteractionEnabled = YES;
imgv2.userInteractionEnabled = YES;
In you handleTapView you need to reorganise slightly. Change the else if in you conditional sequence to if otherwise the third clause will never get triggered.
The recognizer.view for each of the recongnizers will correctly identify the imageView that was tapped. That will be the rect that the popover should present from in the coordinates of the imageView's superview - it's frame property.
So:
[pop presentPopoverFromRect:recognizer.view.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
I'm a junior and I have been really struggling to get this to work, would really appreciate some help.
I am trying to build an App where you can scroll through different WebViews by selecting an item from a table in a popover (while the popover remains in place). Unfortunately, I can't get my scrollView to move when I select an item from the popover view.
I have set up a UIScrollView with page control in a main UIViewController. The scrollView is populated with (pageViewController) UIWebViews. The main UIViewController has a nav bar with a button, when the button is clicked it creates a Popover View. When I select an item from the table in the popover view I want my UIScrollView to scroll to a certain position (equates to a selected page), but the scrollView does not move.
The method below takes the value selected from the popover and uses the page number to determine where the scrollView should scroll to, but it doesn't work because at this point my scrollView is Null.
I can supply more code if needed.
main UIViewController.m code:
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
NSString *pgnum = [[NSString alloc] initWithString:[detailItem description]];
int pg;
if(pgnum == #"Page 1"){
pg =1;
}
if(pgnum == #"Page 2"){
pg =2;
}
if(pgnum == #"Page 3"){
pg =3;
}
[pgnum release];
pageControl.currentPage = pg;
[self loadScrollViewWithPage:pg - 1];
[self loadScrollViewWithPage:pg];
[self loadScrollViewWithPage:pg + 1];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pg;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
}
This is how I triggered the popOverController
The infoButton is on the navbar.
The scrollView is NOT NULL in the infoButton
- (void)infoButton:(id)sender {
NSLog(#"Entering: infoButton");
// [self inspectView:self.view level:#""];
if (self.popoverController == nil) {
PopoverViewController *popoverViewController =
[[PopoverViewController alloc]
initWithNibName:#"PopoverViewController"
bundle:[NSBundle mainBundle]];
popoverViewController.navigationItem.title = #"Navigation";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:popoverViewController];
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
popover.delegate = self;
[popoverViewController release];
[navController release];
self.popoverController = popover;
[popover release];
//NSLog(#"User pressed the info button");
}
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; }
UIPopover, being neither the most obvious class to use memory-management wise, nor the most stable, does not require you to release it before you present it. In fact, doing so causes it to throw a fatal exception! Popovers should be released after they are removed from the screen, and/or in -dealloc.
I would like to present an UIPopover from an UITableView specific index.
Here's my code:
if (indexPath.row == 5) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
EnginesPopoverController *enginesPopoverController = [[EnginesPopoverController alloc] initWithNibName:#"EnginesPopoverController" bundle:nil];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:enginesPopoverController];
self.popoverController = popover;
popoverController.delegate = self;
[popover release];
[enginesPopoverController release];
CGPoint point = {670, 600};
CGSize size = {450, 216};
[popoverController presentPopoverFromRect:CGRectMake(point.x, point.y, size.width, size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
If I try to present the Popover from an UIButton it goes well...
Thanks!
This is what I do:
CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
CGRect popoverRect = CGRectMake(self.view.bounds.size.width - self.popoverController.popoverContentSize.width,
CGRectGetMidY(cellRect),
1.0, 1.0);
The x location I use places the popover on the right-hand side of the screen. This may not be what you want. To find the actual touch point, you can use gesture recognizers.
In return, please try to answer my related question: Popover's arrow to track an object in a scrollview
Manipulating David's code I arrived at this:
(my pop is called "storytPop")- my popover is from the toolbar and I wanted the next tableview to show to the right.
CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
[self.storytPop presentPopoverFromRect:cellRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];