How to add s7graphview to a subview on ViewController nib? - iphone

I have a xib with a subview that I set to Class: S7GraphView (if I select it and click on the identity inspector)
When all I have in the (void) didViewLoad { ...
s7graphView = [[S7GraphView alloc] initWithFrame:[self.view bounds]];
//[self.view addSubview:s7graphView];
s7graphView.dataSource = self;
...
}
In the nib, I control-hold the file's owner to the subview and select - S7GraphView and it still shows up empty in the simulator.
If I comment all this out, I get a black subview (I do set the background to black) but not graph:
// s7graphView = [[S7GraphView alloc] initWithFrame:[self.view bounds]];
// //[self.view addSubview:s7graphView];
// s7graphView.dataSource = self;
// [s7graphView release];
If I remove the subview from my view and instead add it programmatically, it works when I add it like this:
s7graphView = [[S7GraphView alloc] initWithFrame:CGRectMake(20., 70., 280.0, 240.0 )];
[self.view addSubview:s7graphView];
s7graphView.dataSource = self; [s7graphView release];
I should be able to add it via the xib too, right? What do youz think I'm doing wrong?
Thanks!!

Have you implemented the delegate and the datasource?

Related

Populate Table With Help of PopOverController - Objective C

I am using a UIButton, on clicking it I want to display the contents that are present in my NSMutableArray in UITableView with the help of UIPopOverController i.e. I want a UITableView to pop up whose cells show the contents of my NSMutableArray.
I am using the following lines of code:
UITableViewController *table= [[UITableViewController alloc]init];
table.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:table];
self.popoverController = popover;
popoverController.delegate = self;
NSString *hDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *hFilePath = [hisDir stringByAppendingPathComponent:#"hhhh.txt"];
NSArray *array = [NSArray arrayWithContentsOfFile:hFilePath ];
NSMutableArray *kkkk = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
[kkkk addObjectsFromArray:[dict allKeys]];
}
table = [[UIImageView alloc] initWithFrame:[window bounds]];
// Set up the image view and add it to the view but make it hidden
[window addSubview:table];
table.hidden = YES;
[window makeKeyAndVisible];
I am unable to get the UITableView to pop up on the press of my UIButton. Can anyone help me to sort it out?
One way to show the UITableView in the UIPopOverController is by creating a new UIViewController class. And invoking it in initWithContentViewController method of UIPopOverController.
1. Create a new UIViewController or UITableViewController class. Create an instance of it.
2. Use the instance in the initWithContentViewController method of UIPopOverController.
3. Mention from where it should "pop"
For Example in your Button action :
-(IBAction)yourButtonAction:(id)sender
{
YourNewViewController*newVC=[[YourNewViewController alloc]init];
UIPopoverController*somePopOver=[[UIPopoverController alloc]initWithContentViewController:catergoryVC]; //Tell which view controller should be shown
[somePopOver setPopoverContentSize:CGSizeMake(200, 200)]; // set content size of popover
[somePopOver presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; //From where it should "pop"
}
It seems you want to present it from a UIBarButtonItem so instead of presentPopoverFromRect use presentPopoverFromBarButtonItem
[somePopOver presentPopoverFromBarButtonItem:yourBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
If you want to display popover on press of button click, then first add your button in viewcontroller, display that view controller as follows:
In app delegate write code:
MyViewController *viewController = [[MyViewController alloc] init];
[self.window addSubView:viewController.view];
In MyViewController add button and provide target to that button displayPopup as follows:
-(void)displayPopup:(id)sender
{
UITableViewController *tblViewPopover = [[UITableViewController alloc] init];
tblViewPopover.tableView.delegate = self;
tblViewPopover.tableView.dataSource = self;
tblViewPopover.tableView.backgroundColor = [UIColor whiteColor];
tblViewPopover.tableView.separatorStyle= UITableViewCellSeparatorStyleSingleLine;
float theWidth = 280;
tblViewPopover.contentSizeForViewInPopover = CGSizeMake(theWidth,200);
if(m_popOvrController){
[m_popOvrController dismissPopoverAnimated:NO];
[m_popOvrController release];
m_popOvrController=nil;
}
m_popOvrController = [[UIPopoverController alloc] initWithContentViewController:tblViewPopover];
[tblViewPopover release];
[m_popOvrController presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
and you can use tableview delegate methods to display data in tableview.
I think, UIPopoverViewController is initialized using UIViewController and here you are using UIView(UITableView).
Can you please try using UITableViewController instead?
Also, if things does not work according to plan when you create it using the code try using an XIB explicitely.
This should help.

ios5.1 adding a UIView on top of iphone statusbar

I am trying to add a view on top of the statusbar. I have been following this SO post: Adding view on StatusBar in iPhone
For some reason, when I create the window and set Hidden to NO, the view does not appear to show up on top of the statusbar. Does this implementation still work in ios5.1?
Thanks!
This is my custom UIWindow class:
#implementation StatusBarOverlay
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Place the window on the correct level and position
self.hidden = NO;
self.windowLevel = UIWindowLevelStatusBar+1.0f;
self.frame = [[UIApplication sharedApplication] statusBarFrame];
// Create an image view with an image to make it look like a status bar.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [UIImage imageNamed:#"bar_0.png"];
backgroundImageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"bar_1.png"],
[UIImage imageNamed:#"bar_2.png"],
[UIImage imageNamed:#"bar_3.png"],
nil];
backgroundImageView.animationDuration = 0.8;
[backgroundImageView startAnimating];
[self addSubview:backgroundImageView];
}
return self;
}
In my viewcontroller, I created the new window and called this in viewDidLoad:
StatusBarOverlay *overlayWindow = [[StatusBarOverlay alloc] initWithFrame:CGRectZero];
[overlayWindow makeKeyAndVisible];
However, the view still doesn't show up. Any idea as to why?
set your application's window level to UIWindowLevelStatusBar:
self.window.windowLevel = UIWindowLevelStatusBar;
and then add your own view to application window anywhere:
[[[UIApplication sharedApplication]delegate].window addSubview:yourview];
this problem came to me recently, and I just solved it through this way
You can create a new UIWindow and add your view to that. Set the windows frame to [[UIApplication sharedApplication] statusBarFrame] and call makeKeyAndVisible to make it visible.
In the end, I subclassed UIWindow and made that the primary UIWindow in the application AppDelegate. I added any custom view and set the window level to UIWindowLevelStatusBar to display on top of the status bar.

Two switches in a window on iPhone

I've just started learning how to develop an iPhone app.
I'm trying to make an app with two switches. I made two classes (Switch1 & Switch2).
First, I tested the app with one switch (Switch1), and the app worked. But when I made the second class (Switch2) and I Build/Run the app, the first switch (Switch1) disappeared, and what I saw just the second switch (Switch2).
After that I made the background of the (Switch1 & Switch2) celarColor, I could see both of switches. However, the first switch (Switch1) can't be switched.
so I think my problem is how to make both switches (Switch1 & Switch2) visible and working at the same time in the "window"
The question (could be stupid): What can I make them visible and working at the same time?
I think the problem in the following code: This is from the AppDelegate
UIScreen *s1 = [UIScreen mainScreen];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
[window addSubview: view1];
[window makeKeyAndVisible];
UIScreen *s2 = [UIScreen mainScreen];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s2.bounds];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
Here is the Switch1.h
#import
#interface Switch1 : UIView {
UISwitch *mySwitch1;
}
#property (nonatomic, retain) IBOutlet UISwitch *mySwitch1;
#end
Here is the Switch1.m
#import "Switch1.h"
#implementation Switch1
#synthesize mySwitch1;
- (id) initWithFrame: (CGRect) frame {
if ((self = [super initWithFrame: frame])) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
mySwitch1 = [[UISwitch alloc] initWithFrame: CGRectZero];
if (mySwitch1 == nil) {
[self release];
return nil;
}
mySwitch1.on = NO; //the default
[mySwitch1 addTarget: [UIApplication sharedApplication].delegate
action: #selector(valueChanged:)
forControlEvents: UIControlEventValueChanged
];
CGRect b1 = self.bounds;
mySwitch1.transform = CGAffineTransformMakeScale(2, 2);
mySwitch1.center = CGPointMake(
b1.origin.x + b1.size.width / 2,
b1.origin.y + b1.size.height / 2
);
[self addSubview: mySwitch1];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect {
// Drawing code
}
*/
- (void) dealloc {
[mySwitch1 release];
[super dealloc];
}
#end
You might want to configure a view controller with a view and then toss your two switches on that first. Do you understand the MVC patterns here: https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
and here's the view controller guide: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html.
When you're initializing with UIScreen, you're making both switches the same size (the window size) and thus switch 2 is over switch 1 since it's initialized second.
So you are adding one screen (s2) on top of another screen (s1) and therefore you are not able to access s1. You need to make the size of s2 and s1 smaller so that they do not take the whole screen size.
Also by saying makeKeyAndVisible you make the window visible and able to accept user interaction. No need to say that twice.
You're resetting the window when you call
window = [[UIWindow alloc] initWithFrame: s2.bounds];
s1 isn't there anymore because you've created a new window over it. You could just do
UIScreen *s1 = [UIScreen mainScreen];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
[window addSubview: view1];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
If you're just starting, definitely check out this tutorial on iPhone dev. It shows how to use UIViewController, UIView, and a lot of the supplied classes for the iPhone like UITableView and UIImageView.

SearchBar in table view with searchDisplayController programmatically no edit

I'm trying to create a tableview with a searchbar inside the header view of the table. I'd like to use a searchDisplayController to manage everything.
I've created everything programmatically (I'm not feeling comfortable with IB) trying to set all the correct properties, but it seems that I'm missing something, because when the table shows up I'm not able to edit the text in the searchbar and see any animation.
Here is a part of the code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBarTMP=[[UISearchBar alloc]init];
self.searchBar=searchBarTMP;
[searchBarTMP release];
self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
self.searchBar.delegate=self;
self.searchBar.showsScopeBar=YES;
self.searchBar.keyboardType=UIKeyboardTypeDefault;
self.searchBar.userInteractionEnabled=YES;
self.searchBar.multipleTouchEnabled=YES;
self.searchBar.scopeButtonTitles=[NSArray arrayWithObjects:NSLocalizedString(#"City",#"Scope City"),NSLocalizedString(#"Postal Code",#"Scope PostalCode"),nil];
self.tableView.tableHeaderView=searchBar;
self.searchBar.selectedScopeButtonIndex=0;
self.navigationItem.title=NSLocalizedString(#"Store",#"Table title");
//SearchDisplayController creation
UISearchDisplayController *searchDisplayControllerTMP = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController=searchDisplayControllerTMP;
[searchDisplayControllerTMP release];
self.searchDisplayController.delegate=self;
self.searchDisplayController.searchResultsDelegate=self;
self.searchDisplayController.searchResultsDataSource=self;
//....continue
}
I know that when you use a searchbar alone you must deal with its delegate protocol, but I'm guessing that the searchDisplayController manage for you as seen in the Apple sample code. (build up with IB).
Any suggestion?
Thank you,
Andrea
Found it...
After putting in the header of the table view must write
[self.searchBar sizeToFit];
If you are using ARC, make sure you create an iVar for the UISearchDisplayController in your header file.
If you create an UISearchDisplayController using:
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
it will get released by ARC, it will not call any delegate methods and when you'll call self.searchDisplayController (the UIViewController's property) it will be nil.
So, the fix is:
In your header (.h) file:
#interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
UISearchDisplayController* searchDisplayController;
UISearchBar *searchField;
UITableView* tableView;
NSArray* searchResults;
}
and in the implementation (.m) file:
searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
searchField.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
tableView.tableHeaderView = searchField;
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
When implemented like that, you can call both self.searchDisplayController and searchDisplayController in the rest of your code.

add subview loaded from a nib into a frame

I want to add a view from a nib file as a subview of my main view at the click of a button, but it has to be in a frame (0,108,320,351). I've tried the following code:
-(IBAction) displayJoinmeetup:(id)sender
{
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup initWithFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
This displays only a blank screen, otherwise if I remove the frame the subview displays covering the whole screen, how can I properly do this?
You should never call init... on an object after it has been created. If you can change a setting, there will be a setter for it. In this case, it is setFrame:
-(IBAction) displayJoinmeetup:(id)sender {
[meetups removeFromSuperview]; //removing the older view
CGRect myFrame = CGRectMake(0, 108,320,351);
[joinmeetup setFrame:myFrame]; //joinmeetup declared in header file as IBoutlet UIView*joinmeetup
[self.view addSubview:joinmeetup];
}
initWithFrame is not called for a view loaded from nib. See the UIView class ref for details.
You need to set the view size properties in interface builder (inspector).
You can try thi as follow:
-(IBAction) displayJoinmeetup:(id)sender
{
if(meetups)
{
[meetups removeFromSuperview]; //removing the older view
//also here you should release the meetups, if it is allocated
meetups = nil;
}
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 108,320,351)];
joinmeetup = tempView;
[tempView release];
[self.view addSubview:joinmeetup];
}
I suggest you try this:
[joinmeetup setFrame:CGRectMake(0, 108,320,351)];
[self.view addSubview:joinmeetup];