Programmatically drawn UIView tags (or something alike) - objective-c

I'm drawing a view that contains an xib, multiple times (same uiview) and then update the outlets each time it gets drawn displaying an nsmutablearray with different strings on the outlets
Clicking the view opens a viewcontroller that should be displaying the data from the view (+more), this works perfectly fine except it doesn't know which index of the array it's supposed to be displaying.
I'm trying to figure out a way to manage them in a way that I can see which view is being pressed so I can pass that 'id' onto the new vc (something to uniquely identify the view even though it's the same view being drawn)
Here's how the view is being drawn multiple times
- (void) populateUpcoming:(int)events {
[self resetVariables];
upcomingEventsCenterPos = self.view.frame.size.width / 2 - 159;
for (int i = 0; i < events; i++) {
upcomingEventsY2 = 175 * upcomingEvents2;
UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:#"UpcomingEventFull" owner:self options:nil] firstObject];
[_scrollView addSubview:firstViewUIView];
CGRect frame = firstViewUIView.frame;
frame.origin.y = 9 + upcomingEventsY2;
frame.origin.x = upcomingEventsCenterPos + upcomingEventsX2;
firstViewUIView.frame= frame;
upcomingEvents2++;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapUpcomingEvent:)];
[firstViewUIView addGestureRecognizer:singleFingerTap];
}
[self setupScroll];
}

I think you can use UITableView and custom UITableViewCell for this purpose. On click of "Add More", add one entry to NSMutableArray and just reload tableview.
And no need to manage index separately, you can get it directly by indexPath.row
Also you don't need to manage scroll view size as tableview automatically adjust is content height according to row.
If you are using same kind of view,then go with this approach, as its easy and will save your time.

Related

how to change the height of two views (One view shrink and the other expand) using objective c?

i have two views(A&B) in the same view controller(popOver window) and want to toggle between them, view A is larger than view B, when the popOver window first loads i want view A only to appear and when i press certain button inside it , it should shrink down to let view B appear above it, i tried to set the height of view B to fit into the all window when the window first load(Inside viewDidLoad)then reduce it's height and increase the height of the other view when a button is clicked, i try that code but it didn't change the height of the view !!
-(void)viewDidLoad
{
[super viewDidLoad];
UIView *contactsView=[self.view viewWithTag:2];// is this a correct initialization of a uiView from a view in a storyBoard ?
CGRect frame=contactsView.frame;
frame.size.height+=100;
contactsView.frame=frame;
}
any idea about that will be helpful,thanks.
As a general answer (I don't have time for more detail), I would use 3 views overall:
1) The view owned by your View Controller.
2) View A
3) View B
View A and B can be created in viewDidLoad of your view controller. Then set the frame of each of these for the position and size. Add them both to the view:
// in your .h file create these variable:
UIView* m_vA;
UIView* m_vB;
- (void)viewDidLoad
{
m_vA = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)] autorelease];
m_vB = [[[UIView alloc] initWithFrame:<your rect for View B>] autorelease];
// The views will automatically be retained.
[self.view addSubview:vA];
[self.view addSubview:vB];
m_vB.hidden = YES;
}
// In your button callback, toggle the visibility of views
- (IBAction)buttonPressed:(id)sender
{
// Toggle the visibility of views
m_vA.hidden = !m_vA.hidden;
m_vB.hidden = !m_vB.hidden;
}
// Dont forget to clean up the memory of these views in dealloc and viewDidUnload.

Advice for horizontal scroll view IOS

I'm trying to create horizontal scroll view(s). Each view containing one image. [done]
Initially when app is opened I'd display couple of images i.e 3, so user can scroll back and forth between images. [done]
However I want to be able to go to another view controller and pick another image(s) maybe two for example and display five images in the scroll view instead of displaying 3 initially.
How would one do that? to "re-refresh" the initial scroll view ?
Update
Should I use delegate for this communication between view controllers? or how is this done?
1 main controller, other containing image selection?
This part above and much more explained by article here.(not advertising, I hope it will help someone as well).
Bounty update part 1 :
I think now that I found my way around delegates, I have additional question that I cannot find answer to, all examples I saw were about updating table views.
Bounty part 2 :
If I were to have a scrollview inside my view controller and some nsimages inside scroll view. i.e :
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *images = ...some images array;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [images objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}
And let say my view controller implemented some delegate method didAddImage or didRemoveImage.
Meaning that about images array would get updated.
Actual bounty question :
How would one actually "tell" the view controller, O.K now your scrollview has one more image to display, please re-fresh or reload?
If I were to have a table view instead scroll view and was inserting images I'd do it something like this(in my delegate method) :
-(void) mockDelegatetablemethod:(...) ...{
[self.images addObject:image];
NSIndexPath *indexPath =
[NSIndexPath indexPathForRow:[self.images count] - 1
inSection:0];
[self.tableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
How would one do this to the scrollview?
Bounty update part 3:
This is "simple" case described above, naturally I'd have to support removing images also as well as if one wants to remove all images and add some new.
How would one actually "tell" the view controller, O.K now your
scrollview has one more image to display, please re-fresh or reload?
Well, you would just add the new image as a subview of the scroll view. So something like:
- (void)addImage:(UIImage*)image {
[images addObject:image];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * images.count;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = image;
[self.scrollView addSubview:subview];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}
Or you could have a method that goes and "resets" the scroll view by removing all subviews and then re-adds all the images in your images array. Then call this method in viewDidLoad instead of your code there so that you're not duplicating code.
Basically, there's no silver bullet here. You roll your own.
You're going to need make your image scroller a class that you can tell to load up certain images. You'll need a reference to the existing image scroller, pass that pointer into your other "pick more images" controller. Then the "pick more images" controller can tell the image scroller that there is a new list of images.
This is already implemented by Apple in their iOS sample project "PhotoScroller". It has view reuse as well. They even have a WWDC 2010 video on this. I think its advanced scrollview techniques OR designing apps with scrollviews.
Shouldn't be hard to port over to mac

Add lots of views to NSScrollView

I'm trying to add one subview (view from an NSViewController) for every element in a dictionary to a NSScrollView to get kind of a tableview, but with much more flexibility over the cells.
Is it possible to place (programmatically) e.g. 100 subviews underneath each other so that you have to scroll down the NSScrollView to get to the last element?
The short answer is yes. I have done this before, and I assure you that it will work. Let me also assure you that it is not quite as simple as it looks ;)
The way to do this is to maintain one content view, in which you add all of your custom rows as subviews programmatically. Note that you will either have to resize the contentView before adding all of the rows, or you will have to turn off autoresizing for the row views. Set the documentView of your scrollView to this custom contentView, and you should be good to go.
Yes, simply initialize the views programmatically using (i.e.)
NSView *subView = [[NSView alloc] initWithFrame:CGRectMake(10,10,100,100)];
then add to the main using addSubview: method of the main view.
Remember to manually release the subview when you've done with it (that means, when you have added it to the main view).
As example you can do something like
int height x = 10, y = 10, width = 100, height = 100;
for(int i = 0;i<100;i++) {
NSView *subView = [[NSView alloc] initWithFrame:CGRectMake(x,y + height*i,width,height)];
[scrollView addSubview:subView];
[subView release];
}

Objective C: How to set the subview display in the current visible frame? (UITableView)

I have a UITableViewController and intend to add a subview to it when I click a button i.e. refresh button. My code is as follows:
//set up loading page
self.myLoadingPage = [[LoadingPageViewController alloc]init ];
self.myLoadingPage.view.frame = self.view.bounds;
self.myLoadingPage.view.hidden = NO;
[self.view addSubview:self.myLoadingPage.view];
My question is how can I set this subview to be in the current visible frame? especially for a UITableviewcontroller where I might click on the refresh button after scrolling down to the 100th cell, for this example, my subview will still be added right at the top of the table view (starting from cell 1). Is there any way to resolve this?
Just move the lines around so that you set the frame after you made it a subview
self.myLoadingPage = [[LoadingPageViewController alloc]init ];
self.myLoadingPage.view.hidden = NO;
[self.view addSubview:self.myLoadingPage.view];
self.myLoadingPage.view.frame = self.view.bounds;

IOS: fill a NSArray with UIImageView

In my project I created 30 small UIImageView where inside I put a different background. This is my group of UIImageView without background
and this is the group of UIImageView with different background:
I put these group of UImageView inside a UITableViewCell and I use them to define a timeline, but it's not important.
I declared in .h these UIImageView but now I must insert theme in an NSArray, or NSMutableArray? Because in my code (depending on the case) I must set different background for each UIImageView. How can I organize them in my code?
You don't want to put them all in an array. What you actually want to do is assign 'tags' to each view. Then, you can use -[UIView viewWithTag:(int)]; to get a pointer to the image view you want to 'talk' to at the time. Keep in mind not to use negative numbers or 0 when tagging your views.
Although I would suggest drawing your timeline view using a single UIView, dynamically creating and storing views in an array is simple:
NSMutableArray *views = [NSMutableArray array];
NSUInteger viewCount = 30;
NSUInteger index;
for (index = 0; index < viewCount; index++) {
UIImageView *newView = [[[UIImageView alloc] initWithImage:anImage] autorelease];
[newView setFrame:CGRectMake(index * width, 0.0, width, height);
[[self someView] addSubview:newView];
[views addObject:newView];
}
Remember to retain or copy views at some point.
Then, to modify a view:
[[views objectAtIndex:8] setImage:anotherImage];